Named arguments gem

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( :one, :dos => :two, :tres => :three )
=> [:one,: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/

On Oct 26, 9:29 pm, Macario O. [email protected] 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

Daniel B. wrote:

On Oct 26, 9:29�pm, Macario O. [email protected] 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

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.

On Oct 27, 6:09 pm, Macario O. [email protected] 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

Daniel B. wrote:

On Oct 27, 6:09�pm, Macario O. [email protected] 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.

Macario O. wrote:

Daniel B. wrote:

On Oct 27, 6:09�pm, Macario O. [email protected] 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.

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

Roger P. 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.

Google Code Archive - Long-term storage for Google Code Project Hosting.

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

On Oct 27, 9:58 pm, Macario O. [email protected] 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. :slight_smile:

Regards,

Dan

On Oct 29, 2:17 pm, Macario O. [email protected] 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

Daniel B. wrote:

On Oct 27, 9:58�pm, Macario O. [email protected] 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. :slight_smile:

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

Daniel B. wrote:

On Oct 29, 2:17�pm, Macario O. [email protected] 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/