[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Problem with hash in function

Skave Rat

7/10/2008 4:49:00 AM

Somehow I'm missing something really basic. Why is following function
now working?

def foobar(args)
puts args.id
end

foobar ({:id => "MyID"})
#=> some random number (e.g. -616990888)
--
Posted via http://www.ruby-....

3 Answers

Srijayanth Sridhar

7/10/2008 5:25:00 AM

0

args.id will return your object_id. I am not sure that's what you
want. You want:

puts args[:id]

Jayanth

On Thu, Jul 10, 2008 at 10:18 AM, Skave Rat <skaverat@rf-linux.org> wrote:
> Somehow I'm missing something really basic. Why is following function
> now working?
>
> def foobar(args)
> puts args.id
> end
>
> foobar ({:id => "MyID"})
> #=> some random number (e.g. -616990888)
> --
> Posted via http://www.ruby-....
>
>

Michael Glaesemann

7/10/2008 5:26:00 AM

0


On Jul 10, 2008, at 12:48 AM, Skave Rat wrote:

> Somehow I'm missing something really basic. Why is following function
> now working?
>
> def foobar(args)
> puts args.id
> end
>
> foobar ({:id => "MyID"})
> #=> some random number (e.g. -616990888)

Are you expecting foobar({:id => "MyID"}) to return "MyID"? args.id
will return the object id of args (though this is deprecated. I
suspect you want args[:id] instead.

>> def foobar(args)
>> puts args.id
>> end
=> nil
>> foobar ({:id => "MyID"})
(irb):2: warning: Object#id will be deprecated; use Object#object_id
1780410
=> nil
>> def foobad(args)
>> puts args[:id]
>> end
=> nil
>> foobad({:id => "MyID"})
MyID
=> nil

Michael Glaesemann
grzm seespotcode net




Skave Rat

7/10/2008 5:30:00 AM

0

thanks, I knew I missed womething.

Works fine now
--
Posted via http://www.ruby-....