简体   繁体   中英

Printing values from hash returns nil in ruby

I have the following code:

person1 = { :first => "bob", :last => "perry"}
person2 = { :first => "fred", :last => "perry"}
person3 = {:first => "jane", :last =>"perry"}
family = {:dad => person1, :son => person2, :mum => person3}

puts (family[:dad][:last],family[:dad][:first])

It prints out:

perry
bob
nil

Why does it also output nil?

Because you're in irb , which prints out the return value of the last method run, and puts returns nil.

(Or in rails c .)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM