Named arguments gem

M

Macario Ortega

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/
 
D

Daniel Berger

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/

Hm, using your example with named_arguments 0.0.5 I get:

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

Regards,

Dan
 
M

Macario Ortega

Daniel said:
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
 
M

Macario Ortega

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.
 
D

Daniel Berger

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
 
M

Macario Ortega

Daniel said:
Yes, of course.


This works fine.


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.
 
R

Roger Pack

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
 
M

Macario Ortega

Roger said:
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
 
D

Daniel Berger

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
 
M

Macario Ortega

Daniel said:
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
 
D

Daniel Berger

com/maca/namedarguments/tree/master/
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. :)

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
 
M

Macario Ortega

Daniel said:
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/
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,153
Members
46,699
Latest member
AnneRosen

Latest Threads

Top