简体   繁体   中英

Child Classes inheriting included module method from parent classes in Ruby

I am taking a class and they have given me my first attempt at using a module in ruby for a "Bank Account" program.

Here is the question..

Create a module called Interest. Interest should have a method calculate_interest(balance, months, interest_amount) that sets the new balance of the account. It multiples the number of months that have accrued by the interest amount, adds it to the balance, and returns the new balance.

Let's also create a method compound_interest that takes no parameters. Both SavingsAccount and CheckingAccount should implement compound_interest. Interest on a CheckingAccount is $10 each month and accrues every three months. Interest on a SavingsAccount is $5 each month and accrues every six months. Use Interest.calculate_interest in your compound_interest methods to set the new balance on the account.

Then I am given this code...

module Interest
end

class BankAccount
  include Interest
end

class CheckingAccount < BankAccount
end

class SavingsAccount < BankAccount
end

I have been trying for hours to figure out why I can't get it to work. Here is what I have so far.

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

I am very new to this so I'm sure my code is pretty basic, please excuse that. Anyway I keep getting this error...

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)>'

If my child classes are inheriting the module method, I don't understand how the module method can't be found when called on them.

For further reference here are the specs

    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

Sorry for being so lengthy, I just want to give you the same information I have.

this entry (and the other point where calculate_interest is called)...

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

This is trying to call a method, belonging to Interest module.

...but there's no such method. In fact, calculate_interest was included in your classes via the module and became an instance method (a method belonging to every instance of the class).

The correct way to call it from within another instance method is without a receiver

balance = calculate_interest(balance, 3, 10)

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