Problem with hash in function

S

Skave Rat

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)
 
S

Srijayanth Sridhar

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

puts args[:id]

Jayanth
 
M

Michael Glaesemann

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

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

No members online now.

Forum statistics

Threads
474,202
Messages
2,571,057
Members
47,664
Latest member
RoseannBow

Latest Threads

Top