Code Monkey home page Code Monkey logo

arguments's Introduction

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

  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 rdp-arguments

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.