2015-11-19 97 views
-1

我正在上課,他們給了我第一次嘗試在Ruby中使用「Bank Account」程序的模塊。子類繼承包含Ruby中父類的模塊方法

這裏的問題..

創建一個名爲興趣模塊。利息應該有一個方法calculate_interest(balance,months,interest_amount)來設置賬戶的新餘額。它將利息金額累計的月數乘以,並將其加到餘額中,並返回新的餘額。

我們還創建一個不帶參數的方法compound_interest。 SavingsAccount和CheckingAccount都應該執行compound_interest。 CheckingAccount的利息每月10美元,每三個月累計一次。 SavingsAccount的利息每月爲5美元,每六個月累計一次。在您的compound_interest方法中使用Interest.calculate_interest來設置帳戶上的新餘額。

然後我給這個代碼...

module Interest 
end 

class BankAccount 
    include Interest 
end 

class CheckingAccount < BankAccount 
end 

class SavingsAccount < BankAccount 
end 

我一直試圖小時弄清楚爲什麼我不能得到它的工作。 這是我到目前爲止。

module Interest 
    def calculate_interest(balance, months, interest_amount) 
    balance += (months*interest_amount) 
    balance 
    end 
end 

class BankAccount 
    attr_accessor :balance 

    include Interest 

    def initialize(balance) 
    @balance = balance 
    end 
end 

class CheckingAccount < BankAccount 
    def initialize(balance) 
    super(balance) 
    end 

    def compound_interest 
    balance = Interest.calculate_interest(balance, 3, 10) 
    balance 
    end 
end 

class SavingsAccount < BankAccount 
    def initialize(balance) 
    super(balance) 
    end 

    def compound_interest 
    balance = Interest.calculate_interest(balance, 6, 5) 
    balance 
    end 

end 

我對此很新,所以我確定我的代碼是非常基本的,請原諒。 無論如何,我不斷收到這個錯誤...

NoMethodError 
undefined method `calculate_interest' for Interest:Module 
exercise.rb:24:in `compound_interest' 

exercise_spec.rb:31:in `block (3 levels) in <top (required)>' 

如果我的孩子類繼承了模塊的方法,我不知道如何對他們調用的時候,模塊方法不能發現。

更多參考此處的規範

describe BankAccount do 
    describe "initialization" do 
    it "takes an initial balance" do 
     acc = BankAccount.new(283) 
     expect(acc.balance).to eq(283) 
    end 
    end 

    describe "#new" do 
    it "returns the balance" do 
     acc = BankAccount.new(283) 
     expect(acc.balance).to eq(283) 
    end 
    end 
end 

describe CheckingAccount do 
    it "has BankAccount as its parent class" do 
    expect(CheckingAccount.superclass).to eq(BankAccount) 
    end 

    it "includes the module Interest" do 
    expect(CheckingAccount.ancestors).to include(Interest) 
    end 

    describe "#compound_interest" do 
    it "compounds the balance by the specified interest rate" do 
     acc = CheckingAccount.new(283) 
     new_balance = acc.compound_interest 
     expect(new_balance).to eq(283 + 3 * 10) 
     end 
    end 
end 

describe SavingsAccount do 
    it "has BankAccount as its parent class" do 
    expect(SavingsAccount.superclass).to eq(BankAccount) 
    end 

    it "includes the module Interest" do 
    expect(SavingsAccount.ancestors).to include(Interest) 
    end 

    describe "#compound_interest" do 
    it "compounds the balance by the specified interest rate" do 
     acc = SavingsAccount.new(283) 
     new_balance = acc.compound_interest 
     expect(new_balance).to eq (283 + 6 * 5) 
    end 
    end 
end 

對不起,這麼長時間,我只是想給你我有同樣的信息。

+0

提示:什麼是對象的規格調用'compound_interest'上? *你*調用'compound_interest'的對象是什麼?什麼是您打電話給'compound_interest'的對象的類?那個類或它的任何祖先是否有一個'compound_interest'方法? –

+0

不幸的是,「冗長」與「特定」並不相同,實際上恰恰相反。請參閱:[如何創建最小,完整和可驗證示例](http://stackoverflow.com/help/mcve)。在你的問題中有許多無關的絨毛與手頭的問題無關,並且很難看出* actual *問題在哪裏。(我只是偶然碰巧碰到它。) –

+0

你失去了我。我也不太瞭解規格。 –

回答

0

這個條目(和其他地步calculate_interest稱)...

balance = Interest.calculate_interest(balance, 3, 10) 

這是試圖調用一個方法,屬於Interest模塊。

...但沒有這樣的方法。實際上,calculate_interest通過模塊包含在您的課程中,併成爲instance方法(屬於該類的每個實例的方法)。

正確的方法從另一個實例方法中調用它是沒有一個接收器

balance = calculate_interest(balance, 3, 10) 
+0

我想我很困惑,因爲這些說明告訴我在這些方法中使用Interest.calculate_interest。我嘗試過這個解決方案,但仍然失敗並出現新的錯誤。 '''NoMethodError' '未定義方法'+'爲零:NilClass''' –

+0

是的,恭喜你,你已經過了第一個錯誤!看起來'balance'包含nil,所以你不能給它添加一個數字。嘗試引用attr_accessor方法而不是創建局部變量。 'self.balance = calculate_interest(self.balance,3,10)' – SteveTurczyn

+0

我想通了。我需要在模塊方法中使用@balance實例變量而不是「balance」。隨着你的建議,它的工作。謝謝! –