家长班:
account.rb
class Account
attr_reader :owner, :interest
attr_accessor :funds
def initialize(args)
@funds = args[:funds]
@owner = args[:owner]
@interest = 0.01
end
end
saving_account.rb
class SavingAccount < Account
attr_reader :interest
def initialize(args)
@interest = 0.05
super(args)
end
end
当我跑步时
saving_acc = SavingAccount.new(funds: 10000, owner: "Jack")
assert_equal(0.05, saving_acc.interest)
我明白了:
NoMethodError:未定义的方法
interests' for #<SavingAccount:0x007fa711853828> tests/saving_account_test.rb:13:in
test_interests'
我做错了什么?
答案 0 :(得分:5)
更改作业前调用super
的顺序。
class SavingAccount < Account
attr_reader :interest
def initialize(args)
super(args)
@interest = 0.05
end
end
顺便说一下,这是导致错误的拼写错误。
assert_equal(0.05, saving_acc.interests)
^
除此之外,@interest
不是类变量,而是实例变量。
答案 1 :(得分:0)
删除结尾的&#39;
saving_acc = SavingAccount.new(funds: 10000, owner: "Jack")
assert_equal(0.05, saving_acc.interest)