Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8838869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:06:26+00:00 2026-06-14T10:06:26+00:00

I have this class: class Account attr_accessor :balance def initialize(balance) @balance = balance end

  • 0

I have this class:

class Account
    attr_accessor :balance
    def initialize(balance)
            @balance = balance
    end
    def credit(amount)
            @balance += amount
    end
    def debit(amount)
            @balance -= amount
    end
end

Then, for example, later in the program:

bank_account = Account.new(200)
bank_account.debit(100)

If I call the debit method with the “-=” operator in it (as shown in the class above) the program fails with the following message:

bank2.rb:14:in `debit': undefined method `-' for "200":String (NoMethodError)
from bank2.rb:52:in `<main>'

But if I remove the minus sign and just make it @balance = amount, then it works. Obviously I want it to subtract, but I can’t figure out why it doesn’t work. Can math not be done with instance variables?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T10:06:27+00:00Added an answer on June 14, 2026 at 10:06 am

    Your value passed into initialize() is a string, rather than an integer. Cast it to an int via .to_i.

    def initialize(balance)
       # Cast the parameter to an integer, no matter what it receives
       # and the other operators will be available to it later      
       @balance = balance.to_i
    end
    

    Likewise, if the parameter passed to debit() and credit() is a string, cast it to an int.

    def credit(amount)
        @balance += amount.to_i
    end
    def debit(amount)
        @balance -= amount.to_i
    end
    

    Finally, I’ll add that if you plan to set @balance outside the initialize() method, it is recommended to define its setter to call .to_i implicitly.

    def balance=(balance)
      @balance = balance.to_i
    end
    

    Note: This assumes you want and only intend to use integer values. Use .to_f if you need floating point values.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following module/class: module Pigeons class FedEx attr_accessor :signature_name def initialize(account) self.account
Say I have this class: public class Account { public int AccountID { get;
I have this class: class View(object): def main_page(self, extra_placeholders = None): file = '/media/Shared/sites/www/subdomains/pypular/static/layout.tmpl'
can somebody please explain my mistake, I have this class: class Account { private:
I have a domain model that looks like this: case class Account(id: Int, name:
I have this class: Class B { private String D; private String E; }
I have this class file call SMSHelper.cs First I just wanted to know is
I have this class in which I run a for loop 10 times. This
I have this class which i would like to map: public class Contract {
I have this class: class OriginalClass { string FirstItem; List<string> ListOfSecondItems; } I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.