[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: hash as paramerter container

James Gray

1/9/2006 2:06:00 PM

On Jan 9, 2006, at 7:53 AM, Schüle Daniel wrote:

> Hello all,
>
> I am looking for Ruby equivalent for this Python Code
>
> >>> def foo(a,b,c):
> .... print "a is ", a
> .... print "b is ", b
> .... print "c is ", c
> ....
> >>> h = {"c":3, "a":1, "b":2}
> >>>
> >>> foo
> <function foo at 0x403d6f7c>
> >>> foo(**h)
> a is 1
> b is 2
> c is 3

Here are some ideas:

>> def show( a, b, c)
>> puts "a is #{a}"
>> puts "b is #{b}"
>> puts "c is #{c}"
>> end
=> nil
>> params = {:a => 1, :b => 2, :c => 3}
=> {:b=>2, :c=>3, :a=>1}
>> show(*params.values_at(:a, :b, :c))
a is 1
b is 2
c is 3
=> nil

That's about the closet I can get what what you posted, I think.

>> def show( args )
>> [:a, :b, :b].each { |key| puts "#{key} is #{args[key]}" }
>> end
=> nil
>> show(:a => 1, :b => 2, :c => 3)
a is 1
b is 2
b is 2
=> [:a, :b, :b]

This one is more Rubyish though, to me. It uses Ruby's almost named
parameter syntax.

Hope that gives you some new ideas.

James Edward Gray II