Forum: Ruby undefined method `key'

0cf784e5eec99ca94b573d83a0193b6a?d=identicon&s=25 Angel Marques (angel21os)
on 2010-01-30 02:36
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
B31e7abd14f1ceb4c4957da08933c630?d=identicon&s=25 Josh Cheek (Guest)
on 2010-01-30 05:33
(Received via mailing list)
On Fri, Jan 29, 2010 at 7:36 PM, Angel Marques
<angel21ccs@hotmail.com>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.
473fd6eb397bcccdadaa7aa52c278d70?d=identicon&s=25 Ismasensei Ismasensei (ismasensei)
on 2014-02-22 00:58
Just came across this bug. Funny, ain't it? =)
never too late:
The real answer is that the function is key? not key
Cc704ce316e72af824ccd38cfa53104b?d=identicon&s=25 Raja Gopalan (arc)
on 2014-02-22 07:13
hi,

But If you want to get all the keys, then you could use "h.keys", this
will return keys as array.
4828d528e2e46f7c8160c336eb332836?d=identicon&s=25 Robert Heiler (shevegen)
on 2014-02-22 22:02
> 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 http://www.ruby-doc.org/core-2.1.0/Hash.html
249c7fd851c5c5ac5a1abdb756472ae1?d=identicon&s=25 Arup Rakshit (my-ruby)
on 2014-02-22 22:48
Josh Cheek wrote in post #884890:
> On Fri, Jan 29, 2010 at 7:36 PM, Angel Marques
> <angel21ccs@hotmail.com>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.

http://ruby-doc.org/core-2.1.0/Hash.html#method-i-key Hash#key method
exist.

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

d.class.instance_methods(:false).grep(/key$/) # => [:key, :each_key]
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.