0

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?

1
  • 1
    months.keys is an Array of keys, and arrays must be subscripted by integers. Commented Dec 21, 2018 at 18:22

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.