Chuchoo
Chuchoo

Reputation: 842

`no implicit conversion of String into Integer` error when trying to get value from hash

I am trying to get Value in a hash using its Key just like below.

#!/usr/bin/ruby

$, = ", "
months = Hash.new( "month" )
months = {"1" => "January", "2" => "February"}

keys = months.keys["1"]
puts "#{keys}"

I get following error

main.rb:7:in `[]': no implicit conversion of String into Integer (TypeError)
    from main.rb:7:in `<main>'

Why am I getting above error?

Upvotes: 0

Views: 169

Answers (1)

Ursus
Ursus

Reputation: 30056

What you're looking for is just

months["1"]

Why are you using keys method? That returns all the keys as an array. And therefore you can access that just with numbers. That's what the error is saying.

Upvotes: 1

Related Questions