maca / arguments

You don't have to wait until Ruby 2.0 to get (named|keyword) arguments. Has been tested with 1.8.6 and 1.9.1. Plausible alternative to options hash.

This URL has Read+Write access

maca (author)
Thu Aug 06 14:20:05 -0700 2009
commit  f1fff97688c7fca6280ef211e8424ba88347b1cb
tree    ea83a1b8ba74e9946c1a7d834a1f8662550302f3
parent  7308a49c86f3bb0960efc708923d141623d1235c
name age message
file History.txt Loading commit data...
file Manifest.txt Thu Aug 06 14:17:56 -0700 2009 should rebuild gem for latest version (0.5.1) now [maca]
file README.rdoc
file Rakefile Thu Aug 06 14:06:30 -0700 2009 fixed issue with gemspec [maca]
file arguments.gemspec
directory lib/
directory spec/
README.rdoc

arguments

Keyword arguments support now!

DESCRIPTION:

You don’t have to wait until Ruby 2.0 to get (named|keyword) arguments support. Arguments has been tested with Ruby 1.8.6 and ruby 1.9.1 and eventually will work with JRuby (if someone is interested in contributing, I guess is possible since merb-action-args works with JRuby)

SYNOPSIS:

    require 'arguments'

    class Example
      def meth(a = :a, b = :b, c = :c)
        [a,b,c]
      end

      named_args_for :meth

      class << self
        def class_method(a = :a, b = :b, c = :c)
          [a,b,c]
        end
        named_args_for :class_method
      end
    end

    nu = Example.new
    nu.meth #=> [:a,:b,:c]
    nu.meth(1, :c => Class) #=> [1,:b,Class]
    nu.meth(:b => nil, :a => 'something') #=> ['something', nil, :c]

    Example.class_method(:b => nil, :a => 'something') #=> ['something', nil, :c]
  • #named_arguments_for is aliased as #named_args_for and #named_args
  • Calling #named_args_for without arguments patches all previously declared methods in scope
  • Any number of method names (Symbol or String) corresponding to existing methods can be passed
  • Methods with no optionals won’t be patched (will behave as usual with no named params)
  • Same for methods with splatted arguments and for native methods (not declared in Ruby)
  • Keyword argument take precedence over argument order:
        nu.meth(10, :a => -10) #=> [-10, :b, :c]
    

LIMITATIONS

  • Performance penalty occurs only in 1.8.6 due to ParseTree use and only while calling Class#named_args_for, penalty while calling the actuall method is neglectable.
  • With Ruby 1.8.6 it can patch methods declared with eval, with 1.9.1 only methods declared in a source file.
  • If last argument is a Hash is taken as an options Hash and not assigned to the argument varible, if you want to pass a has as a last argument use keywords.

TODO

Don’t use last argument (Hash) as options if passed total number of args.

REQUIREMENTS:

Ruby 1.8.6:

  • ParseTree >= 3.0.3
  • Ruby2Ruby 1.1.9

INSTALL:

    sudo gem install maca-arguments –source http://gems.github.com

LICENSE:

(The MIT License)

Copyright © 2009 Macario Ortega

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ‘Software’), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED ‘AS IS’, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.