Fidel Castro Fidel Castro - 1 month ago 6
Ruby Question

assigning values to variables inside an array

coming from PHP so I'm sorry if the question is too easy for yall :/

My code looks like

animals = [
"horse" => "Caballo",
"Dog" => "Perro",
"Cat" => "Gato",
"Hawk" => "Halcon"
]


puts animals[2]


but is not working it aint printing anything on the terminal ... I don't know if this is the right way to do it in ruby or not :/

what I'm trying to do is to reference individual values for example:

Horse in spanish is animals['horse']

Answer

Use {} to define a hash then:

animals = {
  "horse" => "Caballo",
  "Dog" => "Perro",
  "Cat" => "Gato",
  "Hawk" => "Halcon"
}

puts animals["horse"]
#Caballo
Comments