[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Named arguments gem

Macario Ortega

10/27/2008 3:30:00 AM



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/tr...
--
Posted via http://www.ruby-....

12 Answers

Daniel Berger

10/27/2008 8:10:00 PM

0



On Oct 26, 9:29=A0pm, Macario Ortega <maca...@gmail.com> 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( :one, :dos =3D> :two, :tres =3D> :three )
> =3D> [:one,: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/tr...
> --
> Posted viahttp://www.ruby-....

Hm, using your example with named_arguments 0.0.5 I get:

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

Regards,

Dan

Macario Ortega

10/28/2008 12:09:00 AM

0

Daniel Berger wrote:
> On Oct 26, 9:29�pm, Macario Ortega <maca...@gmail.com> wrote:
>> � def another_instance_method( a = :a, b = :b, c = :c)
>> � end
>>
>> http://github.com/maca/namedarguments/tr...
>> --
>> Posted viahttp://www.ruby-....
>
> 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-....

Macario Ortega

10/28/2008 2:37:00 AM

0

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

Daniel Berger

10/28/2008 2:38:00 AM

0



On Oct 27, 6:09=A0pm, Macario Ortega <maca...@gmail.com> wrote:
> Daniel Berger wrote:
> > On Oct 26, 9:29 pm, Macario Ortega <maca...@gmail.com> wrote:
> >> def another_instance_method( a =3D :a, b =3D :b, c =3D :c)
> >> end
>
> >>http://github.com/maca/namedarguments/tr...
> >> --
> >> Posted viahttp://www.ruby-....
>
> > 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

Macario Ortega

10/28/2008 3:06:00 AM

0

Daniel Berger wrote:
> On Oct 27, 6:09�pm, Macario Ortega <maca...@gmail.com> 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-....

Macario Ortega

10/28/2008 3:58:00 AM

0

Macario Ortega wrote:
> Daniel Berger wrote:
>> On Oct 27, 6:09�pm, Macario Ortega <maca...@gmail.com> 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/tr...

and install acording to the Readme file.









--
Posted via http://www.ruby-....

Roger Pack

10/28/2008 3:44:00 PM

0

> 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/Named...

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

Macario Ortega

10/28/2008 5:52:00 PM

0

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/Named...
>
> 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-....

Daniel Berger

10/29/2008 3:32:00 PM

0



On Oct 27, 9:58=A0pm, Macario Ortega <maca...@gmail.com> wrote:
> Macario Ortega wrote:
> > Daniel Berger wrote:
> >> On Oct 27, 6:09 pm, Macario Ortega <maca...@gmail.com> 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://...
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

Macario Ortega

10/29/2008 8:17:00 PM

0

Daniel Berger wrote:
> On Oct 27, 9:58�pm, Macario Ortega <maca...@gmail.com> 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/tr...
>>
>> 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-....