Undefined method `key'

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

On Fri, Jan 29, 2010 at 7:36 PM, Angel M.
[email protected]wrote:

what happens with this method, it’s wrong?
thanks

Posted via http://www.ruby-forum.com/.

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.

Just came across this bug. Funny, ain’t it? =)
never too late:
The real answer is that the function is key? not key

what happens with this method, it’s wrong?

You must look at the official documentation to see if that method exists
or not.

See here Class: Hash (Ruby 2.1.0)

hi,

But If you want to get all the keys, then you could use “h.keys”, this
will return keys as array.

Josh C. wrote in post #884890:

On Fri, Jan 29, 2010 at 7:36 PM, Angel M.
[email protected]wrote:

what happens with this method, it’s wrong?
thanks

Posted via http://www.ruby-forum.com/.

Here are the hash methods: http://ruby-doc.org/core/classes/Hash.html
Notice that “key” is not listed.

Class: Hash (Ruby 2.1.0) Hash#key method
exist.

d = { :c => 100, :f => 200 }
d.key(200) # => :f

d.class.instance_methods(:false).grep(/key$/) # => [:key, :each_key]