Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Named arguments gem

Reply
Thread Tools

Named arguments gem

 
 
Macario Ortega
Guest
Posts: n/a
 
      10-27-2008


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

 
Reply With Quote
 
 
 
 
Daniel Berger
Guest
Posts: n/a
 
      10-27-2008


On Oct 26, 9:29=A0pm, Macario Ortega <(E-Mail Removed)> 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( ne, :dos =3D> :two, :tres =3D> :three )
> =3D> [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

 
Reply With Quote
 
 
 
 
Macario Ortega
Guest
Posts: n/a
 
      10-28-2008
Daniel Berger wrote:
> On Oct 26, 9:29�pm, Macario Ortega <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Macario Ortega
Guest
Posts: n/a
 
      10-28-2008
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/.

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      10-28-2008


On Oct 27, 6:09=A0pm, Macario Ortega <(E-Mail Removed)> wrote:
> Daniel Berger wrote:
> > On Oct 26, 9:29 pm, Macario Ortega <(E-Mail Removed)> 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

 
Reply With Quote
 
Macario Ortega
Guest
Posts: n/a
 
      10-28-2008
Daniel Berger wrote:
> On Oct 27, 6:09�pm, Macario Ortega <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Macario Ortega
Guest
Posts: n/a
 
      10-28-2008
Macario Ortega wrote:
> Daniel Berger wrote:
>> On Oct 27, 6:09�pm, Macario Ortega <(E-Mail Removed)> 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/.

 
Reply With Quote
 
Roger Pack
Guest
Posts: n/a
 
      10-28-2008
> 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-...amedParameters

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

 
Reply With Quote
 
Macario Ortega
Guest
Posts: n/a
 
      10-28-2008
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-...amedParameters
>
> 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/.

 
Reply With Quote
 
Daniel Berger
Guest
Posts: n/a
 
      10-29-2008


On Oct 27, 9:58=A0pm, Macario Ortega <(E-Mail Removed)> wrote:
> Macario Ortega wrote:
> > Daniel Berger wrote:
> >> On Oct 27, 6:09 pm, Macario Ortega <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[ANN] rdp-arguments -- named arguments for 1.8 and 1.9! Roger Pack Ruby 2 01-04-2010 02:56 AM
(named|keyword) arguments gem Macario Ortega Ruby 0 08-07-2009 04:38 AM
RubyGems 0.9.1 calling a gem with gem '<gem>' Austin 7873 Ruby 5 01-27-2007 10:05 PM
Passing variable number of named arguments Ramashish Baranwal Python 2 12-28-2006 05:36 AM
functions and named keyword arguments Fuzzyman Python 2 02-22-2005 08:00 AM



Advertisments