0

So I have an example below :

movies = {
  dobby: "dobster is a lad",
  pirates_of_the_carribean: "Its all about jack sparrow kicking ass!"
  }
puts "what do you want to know about?\n
#{movies[:dobby]}. = 1\n
or...\n
#{movies[:pirates_of_the_carribean]}. = 2\n
Pick a number :"

Now what I get is the value of the key but I am looking to get the Key so that puts will output just the key and not the value.

I understand that the solution will likely output the key as :key and not key so id also like to know how to return a key without the ":" for displaying purposes.

Note : I have done thorough searching using google and haven't found a solution to this issue.

Ahurasim
  • 27
  • 1
  • 8

2 Answers2

1

So I found my answer in a question that was asking something similar. To find just the key I used the .keys[] method my resulting code was this :

movies = {
  dobby: "dobster is a lad",
  pirates_of_the_carribean: "Its all about jack sparrow kicking ass!"
  }
puts "what do you want to know about?\n
#{movies.keys[0]}. = 1\n
or...\n
#{movies.keys[1]}. = 2\n
Pick a number :"

This code prints out the keys without the : and thus for #{movies.keys[1]} it prints out pirates_of_the_carribean.

Ahurasim
  • 27
  • 1
  • 8
0

You can use the keys method in movies hash:

movies = {
  dobby: "dobster is a lad",
  pirates_of_the_carribean: "Its all about jack sparrow kicking ass!"
}

puts "What do you want to know about?" 
counter=0
movies.keys.each do |k|
   counter += 1
   puts "#{k}. = #{counter} "
end 
puts "Pick a number:"
Tiago Lopo
  • 7,619
  • 1
  • 30
  • 51