16/15 Hashes


#1



Here is my code:
my_hash = Hash.new
my_hash["dog"]="woof"
my_hash["cat"]="weow"

The result is 
"weow"

I was wondering why the result isn't like this?
"woof"
"weow"


#2

What you see in the output is the return value of the last evaluated expression. That is why there is "weow".

Change the order of lines:

my_hash = Hash.new
my_hash["cat"]="weow"
my_hash["dog"]="woof"

and you will see "woof".


#3

Thank for your reply
But I mean is it possible to show both weow and woof?


#4

To print something to the console use puts:

my_hash = Hash.new

my_hash["dog"]="woof"
my_hash["cat"]="weow"

puts my_hash["dog"]
puts my_hash["cat"]

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.