person1 = Person.new
person1.first = "Tim"
person1.last = "Happy"
person2 = Person.new
person2.first = "Zack"
person2.last = "Sad"
person3 = Person.new
person3.first = "Al"
person3.last = "Angry"
params = {:father => person1, :mother => person2, :child => person3} #params hash
puts params[:father][:first]
我试图让它只打印蒂姆。我在这里做错了什么?
答案 0 :(得分:3)
Person
似乎是一个类(与Hash
不同),因此除非该类明确定义了[]
访问器,否则无法以此方式访问它(例如,ActiveRecord::Base
does。你想要的是:
puts params[:father].first
这无疑会让人感到困惑,例如JavaScript,这两种符号可以互换。