Named arguments gem

Discussion in 'Ruby' started by Macario Ortega, Oct 27, 2008.

  1. Hi, I've written a small gem to pass named arguments to an existing
    method using ruby2ruby. Here's the usage:

    require 'named_arguments'

    class Example
    def instance_method(uno = 1, dos = 2, tres = 3, cuatro = 4)
    [uno, dos, tres, cuatro]
    end

    def another_instance_method( a = :a, b = :b, c = :c)
    [a,b,c]
    end

    named_args_for :instance_method, :another_instance_method

    class << self
    def class_method(uno = 1, dos = 2, tres = 3, cuatro = 4)
    [uno, dos, tres, cuatro]
    end
    named_args_for :class_method
    end
    end

    Example.new.instance_method( :eek:ne, :dos => :two, :tres => :three )
    => [:eek:ne,:two,:three,4]

    Example.new.another_instance_method
    => [:a,:b,:c]

    Example.class_method( :dos => :b, :cuatro => :d )
    => [1,:b,2,:d]



    http://github.com/maca/namedarguments/tree/master/
    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 27, 2008
    #1
    1. Advertising

  2. On Oct 26, 9:29=A0pm, Macario Ortega <> wrote:
    > Hi, I've written a small gem to pass named arguments to an existing
    > method using ruby2ruby. Here's the usage:
    >
    > require 'named_arguments'
    >
    > class Example
    > =A0 def instance_method(uno =3D 1, dos =3D 2, tres =3D 3, cuatro =3D 4)
    > =A0 =A0 [uno, dos, tres, cuatro]
    > =A0 end
    >
    > =A0 def another_instance_method( a =3D :a, b =3D :b, c =3D :c)
    > =A0 =A0 [a,b,c]
    > =A0 end
    >
    > =A0 named_args_for :instance_method, :another_instance_method
    >
    > =A0 class << self
    > =A0 =A0 def class_method(uno =3D 1, dos =3D 2, tres =3D 3, cuatro =3D 4)
    > =A0 =A0 =A0 [uno, dos, tres, cuatro]
    > =A0 =A0 end
    > =A0 =A0 named_args_for :class_method
    > =A0 end
    > end
    >
    > Example.new.instance_method( :eek:ne, :dos =3D> :two, :tres =3D> :three )
    > =3D> [:eek:ne,:two,:three,4]
    >
    > Example.new.another_instance_method
    > =3D> [:a,:b,:c]
    >
    > Example.class_method( :dos =3D> :b, :cuatro =3D> :d )
    > =3D> [1,:b,2,:d]
    >
    > http://github.com/maca/namedarguments/tree/master/
    > --
    > Posted viahttp://www.ruby-forum.com/.


    Hm, using your example with named_arguments 0.0.5 I get:

    undefined method `named_args_for' for Example:Class (NoMethodError)

    Regards,

    Dan
     
    Daniel Berger, Oct 27, 2008
    #2
    1. Advertising

  3. Daniel Berger wrote:
    > On Oct 26, 9:29�pm, Macario Ortega <> wrote:
    >> � def another_instance_method( a = :a, b = :b, c = :c)
    >> � end
    >>
    >> http://github.com/maca/namedarguments/tree/master/
    >> --
    >> Posted viahttp://www.ruby-forum.com/.

    >
    > Hm, using your example with named_arguments 0.0.5 I get:
    >
    > undefined method `named_args_for' for Example:Class (NoMethodError)
    >
    > Regards,
    >
    > Dan


    When you require the gem it adds the method #named_args_for to any
    object so you can use it while defining a class or later on.

    Have you required the gem with this line?
    require 'named_arguments'

    Please add this line at the top:
    Object.send( :include, NamedArguments )

    If you get this error:
    uninitialized constant NamedArguments

    named_arguments has not been required

    Please let me know how it goes.

    Macario



    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 28, 2008
    #3
  4. 0.5.1

    I pushed a small change.

    I didn't realize only literal arguments could be passed (symbols and
    numbers). I fixed this to make posible passing any kind of argument.
    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 28, 2008
    #4
  5. On Oct 27, 6:09=A0pm, Macario Ortega <> wrote:
    > Daniel Berger wrote:
    > > On Oct 26, 9:29 pm, Macario Ortega <> wrote:
    > >> def another_instance_method( a =3D :a, b =3D :b, c =3D :c)
    > >> end

    >
    > >>http://github.com/maca/namedarguments/tree/master/
    > >> --
    > >> Posted viahttp://www.ruby-forum.com/.

    >
    > > Hm, using your example with named_arguments 0.0.5 I get:

    >
    > > undefined method `named_args_for' for Example:Class (NoMethodError)

    >
    > > Regards,

    >
    > > Dan

    >
    > When you require the gem it adds the method #named_args_for to any
    > object so you can use it while defining a class or later on.
    >
    > Have you required the gem with this line?
    > require 'named_arguments'


    Yes, of course.

    > Please add this line at the top:
    > Object.send( :include, NamedArguments )


    This works fine.

    > If you get this error:
    > uninitialized constant NamedArguments
    >
    > named_arguments has not been required
    >
    > Please let me know how it goes.


    It doesn't work.

    Regards,

    Dan
     
    Daniel Berger, Oct 28, 2008
    #5
  6. Daniel Berger wrote:
    > On Oct 27, 6:09�pm, Macario Ortega <> wrote:
    >>
    >> require 'named_arguments'

    > Yes, of course.
    >
    >> Please add this line at the top:
    >> Object.send( :include, NamedArguments )

    >
    > This works fine.
    >
    >> If you get this error:
    >> uninitialized constant NamedArguments
    >>
    >> named_arguments has not been required
    >>
    >> Please let me know how it goes.

    >
    > It doesn't work.
    >
    > Regards,
    >
    > Dan



    I don't know why it doesn't work. Do the specs fail?
    I will check tomorrow in a fresh machine.

    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 28, 2008
    #6
  7. Macario Ortega wrote:
    > Daniel Berger wrote:
    >> On Oct 27, 6:09�pm, Macario Ortega <> wrote:
    >>>
    >>> require 'named_arguments'

    >> Yes, of course.
    >>
    >>> Please add this line at the top:
    >>> Object.send( :include, NamedArguments )

    >>
    >> This works fine.
    >>
    >>> If you get this error:
    >>> uninitialized constant NamedArguments
    >>>
    >>> named_arguments has not been required
    >>>
    >>> Please let me know how it goes.

    >>
    >> It doesn't work.
    >>
    >> Regards,
    >>
    >> Dan

    >
    >
    > I don't know why it doesn't work. Do the specs fail?
    > I will check tomorrow in a fresh machine.



    Right I think I know whats wrong.

    there is another gem named
    named_arguments 0.0.5 at ruby forge

    Mine is just hosted at github


    If you want to give it a try you have to downolad it from
    http://github.com/maca/namedarguments/tree/master/

    and install acording to the Readme file.









    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 28, 2008
    #7
  8. Macario Ortega

    Roger Pack Guest

    > require 'named_arguments'
    >
    > class Example
    >
    > def another_instance_method( a = :a, b = :b, c = :c)
    > [a,b,c]
    > end
    >
    > named_args_for :instance_method, :another_instance_method


    How fascinating that your and my project would arrive at almost the same
    spot from [seemingly] different angles. LOL.

    http://code.google.com/p/ruby-roger-useful-functions/wiki/NamedParameters

    It would be interesting to compare the two. Perhaps we should combine
    projects.

    One thing to also look out for is if it works appropriately with blocks.

    I think the next step for this type of project is to have it fallback to
    ruby_parser [which just now started to work with 1.9]. We could thus
    use #UnboundMethod.source_location to try and parse the original source.
    Then it could work with 1.9
    Cheers!
    -=R
    --
    Posted via http://www.ruby-forum.com/.
     
    Roger Pack, Oct 28, 2008
    #8
  9. Roger Pack wrote:
    >> require 'named_arguments'
    >>
    >> class Example
    >>
    >> def another_instance_method( a = :a, b = :b, c = :c)
    >> [a,b,c]
    >> end
    >>
    >> named_args_for :instance_method, :another_instance_method

    >
    > How fascinating that your and my project would arrive at almost the same
    > spot from [seemingly] different angles. LOL.
    >
    > http://code.google.com/p/ruby-roger-useful-functions/wiki/NamedParameters
    >
    > It would be interesting to compare the two. Perhaps we should combine
    > projects.
    >
    > One thing to also look out for is if it works appropriately with blocks.
    >
    > I think the next step for this type of project is to have it fallback to
    > ruby_parser [which just now started to work with 1.9]. We could thus
    > use #UnboundMethod.source_location to try and parse the original source.
    > Then it could work with 1.9
    > Cheers!
    > -=R


    Yeah, both solutions look similar. I had a very specific need where I
    had a set of methods where I needed to pass just certain literal (sybols
    and numbers) arguments so at one point I realized my solution worked
    only with literals, I've fixed this using object_id and
    ObjectSpace._id2ref for that object id.

    I've been a Rails user for a while and just recently I had this epiphany
    where spec driven development came into place and i understood what
    metaprogramming was, just when i started to feel all there was ahead was
    convention over configuration.

    I havent tried to make it work with blocks or with ruby 1.9, actually I
    haven't toyed with ruby 1.9 but yeah let's make it work with 1.9 and
    blocks.

    Cheers





    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 28, 2008
    #9
  10. On Oct 27, 9:58=A0pm, Macario Ortega <> wrote:
    > Macario Ortega wrote:
    > > Daniel Berger wrote:
    > >> On Oct 27, 6:09 pm, Macario Ortega <> wrote:

    >
    > >>> require 'named_arguments'
    > >> Yes, of course.

    >
    > >>> Please add this line at the top:
    > >>> Object.send( :include, NamedArguments )

    >
    > >> This works fine.

    >
    > >>> If you get this error:
    > >>> uninitialized constant NamedArguments

    >
    > >>>named_argumentshas not been required

    >
    > >>> Please let me know how it goes.

    >
    > >> It doesn't work.

    >
    > >> Regards,

    >
    > >> Dan

    >
    > > I don't know why it doesn't work. Do the specs fail?
    > > I will check tomorrow in a fresh machine.

    >
    > Right I think I know whats wrong.
    >
    > there is another gem namednamed_arguments=A00.0.5 at ruby forge
    >
    > Mine is just hosted at github
    >
    > If you want to give it a try you have to downolad it fromhttp://github.co=

    m/maca/namedarguments/tree/master/
    >
    > and install acording to the Readme file.


    Aha, thanks, that explains it.

    Could I convince you to change the name then? I mean, you could do
    more than named arguments couldn't you? Also, it would eliminate the
    confusion.

    Could you also add some sort of optional pseudo-static typing that
    would automatically raise a TypeError if the wrong type was given?

    class Example
    def test_method(Fixnum alpha =3D 1, String beta =3D "world")
    [alpha, beta]
    end
    end

    ex =3D Example.new
    ex.test_method:)beta =3D> "Hello", :alpha =3D> 3) # ok
    ex.test_method:)alpha =3D> "Hello", :beta =3D> 3) # TypeError

    If so, consider it a feature request. :)

    Regards,

    Dan
     
    Daniel Berger, Oct 29, 2008
    #10
  11. Daniel Berger wrote:
    > On Oct 27, 9:58�pm, Macario Ortega <> wrote:
    >> >> This works fine.
    >> >> Regards,

    >> Mine is just hosted at github
    >>
    >> If you want to give it a try you have to downolad it fromhttp://github.com/maca/namedarguments/tree/master/
    >>
    >> and install acording to the Readme file.

    >
    > Aha, thanks, that explains it.
    >
    > Could I convince you to change the name then? I mean, you could do
    > more than named arguments couldn't you? Also, it would eliminate the
    > confusion.
    >
    > Could you also add some sort of optional pseudo-static typing that
    > would automatically raise a TypeError if the wrong type was given?
    >
    > class Example
    > def test_method(Fixnum alpha = 1, String beta = "world")
    > [alpha, beta]
    > end
    > end
    >
    > ex = Example.new
    > ex.test_method:)beta => "Hello", :alpha => 3) # ok
    > ex.test_method:)alpha => "Hello", :beta => 3) # TypeError
    >
    > If so, consider it a feature request. :)
    >
    > Regards,
    >
    > Dan



    Yeah, I think I will change the gem name to avoid confusion, about the
    semi-static syntax you propose is not very ruby-ish, the ruby
    interpreter would complain if you define a method like you propose but
    you could define your method like this:

    class Example
    def test_method( alpha = 1, beta = "wold)
    raise TypeError.new("alpha must be an Integer") unless
    alpha.instance_of?(Integer)
    ...do stuff with alpha and beta
    end
    named_args_for :test_method
    end




    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 29, 2008
    #11
  12. On Oct 29, 2:17=A0pm, Macario Ortega <> wrote:
    > Daniel Berger wrote:
    > > On Oct 27, 9:58 pm, Macario Ortega <> wrote:
    > >> >> This works fine.
    > >> >> Regards,
    > >> Mine is just hosted at github

    >
    > >> If you want to give it a try you have to downolad it fromhttp://github=

    com/maca/namedarguments/tree/master/
    >
    > >> and install acording to the Readme file.

    >
    > > Aha, thanks, that explains it.

    >
    > > Could I convince you to change the name then? I mean, you could do
    > > more than named arguments couldn't you? Also, it would eliminate the
    > > confusion.

    >
    > > Could you also add some sort of optional pseudo-static typing that
    > > would automatically raise a TypeError if the wrong type was given?

    >
    > > class Example
    > > =A0 def test_method(Fixnum alpha =3D 1, String beta =3D "world")
    > > =A0 =A0 [alpha, beta]
    > > =A0 end
    > > end

    >
    > > ex =3D Example.new
    > > ex.test_method:)beta =3D> "Hello", :alpha =3D> 3) # ok
    > > ex.test_method:)alpha =3D> "Hello", :beta =3D> 3) # TypeError

    >
    > > If so, consider it a feature request. :)

    >
    > > Regards,

    >
    > > Dan

    >
    > Yeah, I think I will change the gem name to avoid confusion, about the
    > semi-static syntax you propose is not very ruby-ish, the ruby
    > interpreter would complain if you define a method like you propose but
    > you could define your method like this:
    >
    > class Example
    > =A0 def test_method( alpha =3D 1, beta =3D "wold)
    > =A0 =A0 raise TypeError.new("alpha must be an Integer") unless
    > alpha.instance_of?(Integer)
    > =A0 =A0 ...do stuff with alpha and beta
    > =A0 end
    > =A0 named_args_for :test_method
    > end


    Oh, I realize there are ways to get the same behavior. But, I'd like
    that notation.

    What I'm ultimately hoping for is that, if you support that notation,
    we could use RubyInline behind the scenes and do automatic
    optimization:

    http://segment7.net/projects/ruby/inline_optimization.html

    What do you think?

    Regards,

    Dan
     
    Daniel Berger, Oct 29, 2008
    #12
  13. Daniel Berger wrote:
    > On Oct 29, 2:17�pm, Macario Ortega <> wrote:
    >> > Aha, thanks, that explains it.
    >> > � � [alpha, beta]

    >>
    >> alpha.instance_of?(Integer)
    >> � � ...do stuff with alpha and beta
    >> � end
    >> � named_args_for :test_method
    >> end

    >
    > Oh, I realize there are ways to get the same behavior. But, I'd like
    > that notation.
    >
    > What I'm ultimately hoping for is that, if you support that notation,
    > we could use RubyInline behind the scenes and do automatic
    > optimization:
    >
    > http://segment7.net/projects/ruby/inline_optimization.html
    >
    > What do you think?
    >
    > Regards,
    >
    > Dan


    I don't think that notation is actually posible, I guess that is not
    ruby anymore. You could write your method in a string in a ruby like
    syntax with the expected type, parse it and evaluate it to generate a
    method def like the one above but that would defy the purpouse of
    turning any ruby method into a method that accepts an options hash,
    another altenative is to have a method called #named_args_with_typing
    that would accept a hash with a Symbol key corresponding to the argument
    and as value the expected Object the arg should be instance of, and
    generate a code structure like the one above; or just stick to duck
    typing and be careful not to pass the wrong type. Anyway it will blow
    its just matter of the kind of error to expect.

    I've zero experience with inline ruby but what I understand from this
    post

    http://blog.zenspider.com/2005/02/rubytoruby.html

    is that you could programatically generate C from ruby code, I guess it
    should be super plain ruby code. This project seems to "compile" domain
    specific ruby code for the ATMEGA microprocessor:
    http://rad.rubyforge.org/



    --
    Posted via http://www.ruby-forum.com/.
     
    Macario Ortega, Oct 29, 2008
    #13
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Fuzzyman
    Replies:
    2
    Views:
    297
    Fuzzyman
    Feb 22, 2005
  2. Ramashish Baranwal

    Passing variable number of named arguments

    Ramashish Baranwal, Dec 27, 2006, in forum: Python
    Replies:
    2
    Views:
    267
    Ramashish Baranwal
    Dec 28, 2006
  3. Austin 7873
    Replies:
    5
    Views:
    192
    Eric Hodel
    Jan 27, 2007
  4. Macario Ortega

    (named|keyword) arguments gem

    Macario Ortega, Aug 7, 2009, in forum: Ruby
    Replies:
    0
    Views:
    93
    Macario Ortega
    Aug 7, 2009
  5. Roger Pack
    Replies:
    2
    Views:
    109
    Roger Pack
    Jan 4, 2010
Loading...

Share This Page