我对 Stack Overflow 和 Ruby 都很陌生,所以如果我没有正确格式化某些内容,我提前道歉,但我希望在通过对象调用或显示父类中的数组值方面得到一些帮助。
以下代码是我在《Learn Ruby the Hard Way》(练习 42)一书中正在做的一项任务/学习练习:
## Person is-a object
class Person
def initialize(name)
## class Person has-a name
@name = name
## person has-a pet of some kind
@pet = nil
end
@possessions = ['house', 'car', 'clothes', 'furniture', 'guitar']
attr_accessor :pet
attr_accessor :possessions
end
## class Employee is-a Person
class Employee < Person
def initialize(name, salary)
## set the @name attribute from class Person
super(name)
## class Employee has-a salary
@salary = salary
end
tasks = {"emails" => "Must answer all emails right away",
"reports" => "File two reports once a month",
"reimbursement" => "File expenses to get reimbursements"
}
attr_accessor :tasks
end
## Mary is-a person
mary = Person.new("Mary")
## Frank is-a Employee
frank = Employee.new("Frank", 120000)
# Study drill 4
puts mary.possessions[4]
puts frank.tasks["emails"]
以下是我运行脚本时终端返回的内容(基本上是一个空白区域):
Macintosh:mystuff3 Vallish$ ruby ex42d.rb
Macintosh:mystuff3 Vallish$
我认为我的语法有误,或者我创建的数组/哈希不正确,我希望得到一些帮助。
我的目标是基本上尝试将数组中的值和类中的哈希传递给它的相关对象,然后调用这些值。
提前致谢!