undefined method `key'

A

Angel Marques

Hi...I type

h = { "a" => 100, "b" => 200 }
or
d = { :c => 100, :f => 200 }

d.key(200)

and the console show me the next error:
undefined method `key' for {"a"=>100, "b"=>200}:Hash (NoMethodError)

what happens with this method, it's wrong?
thanks
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

Hi...I type

h = { "a" => 100, "b" => 200 }
or
d = { :c => 100, :f => 200 }

d.key(200)

and the console show me the next error:
undefined method `key' for {"a"=>100, "b"=>200}:Hash (NoMethodError)

what happens with this method, it's wrong?
thanks
Here are the hash methods: http://ruby-doc.org/core/classes/Hash.html
Notice that "key" is not listed.
If you look at the methods listed, you will see one called "index" and when
reading it's description, you see:

Returns the key for a given value. If not found, returns nil.
h = { "a" => 100, "b" => 200 }
h.index(200) #=> "b"
h.index(999) #=> nil


Which looks exactly like what you were trying to do.

Generally, you can find your way here from http://ruby-doc.org/ in the
orange section, select which version of Ruby you have, in the middle frame
along the top, select the Hash class, and it will display in the frame at
the bottom. You can also usually just google "ruby hash" and jump right to
the 1.8.6 docs.
 

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
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top