Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

You don't have to wait until Ruby 2.0 to get (named|keyword) arguments. Works with 1.8.6, 1.8.7 and 1.9.1. An alternative to options hash.

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 lib
Octocat-spinner-32 spec
Octocat-spinner-32 .gitignore
Octocat-spinner-32 History.txt
Octocat-spinner-32 Manifest.txt
Octocat-spinner-32 README.rdoc
Octocat-spinner-32 Rakefile
Octocat-spinner-32 arguments.gemspec
README.rdoc

arguments

github.com/maca/arguments

This gem is long dead and buried, after all there is nothing wrong to passing an options hash as people have been doing for ages. It was allways an stubborn big hack, now with Ruby 1.9+ all those amazing possibilities of introspection are gonne. Oh man, it was fun playing with the AST of live objects!! But now we are safer from this kind of voodoo. Off course I could keep on the stubborness, I even foolishly considered and made an attempt to parse Iseq's to reconstruct the AST, but it would allways be a hack. This gem is dead, but it will live forever in the repository, as a friend once told me.

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

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

    def other_class_method(a = :a, b = :b, c = :c)
      [a,b,c]
    end        
    named_args_for :class_method
  end

  named_args_for :meth, :'self.other_class_method'
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]
Example.other_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

  • Last Hash argument is used to assign argument values unless passing all accepted arguments:

    nu.meth(1, :c => 'string') #=> [1, :b, 'string'] nu.meth(1, 2, :a => 'string') #=> [1, 2, {:a => 'string'}]

  • 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]
  • pasing a key not corresponding to an argument name will raise ArgumentError:

    nu.meth(10, :four => -10) #=> ArgumentError: `four` is not an argument name.

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.

REQUIREMENTS:

  • Ruby2Ruby 1.1.9

Ruby 1.8.6:

  • ParseTree >= 3.0.3

Ruby 1.9.1

  • RubyParser >= 2.0.2

INSTALL:

sudo gem install arguments --source http://gemcutter.org

LATEST CHANGES (0.6)

  • Fixed a bug where converting a block to proc argument using ampersand would blow method call

  • Passing a keyword not corresponding to an argument name raises an error

  • Using RubyParser in Ruby 1.9.1 instead of Regexp to extract argument names and default values

  • Method definition that uses splat operator is now ignored

  • Last Hash argument is not used for assigning argument values if total number of arguments has been passed

  • named_arguments_for can be called passing a Class method name eg: named_args_for :instance_method, 'self.class_method'

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.

Something went wrong with that request. Please try again.