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 8319961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:29:36+00:00 2026-06-08T22:29:36+00:00

class Account def initialize(starting_balance = 0) @balance = starting_balance end def balance #instance getter

  • 0
class Account
  def initialize(starting_balance = 0)
     @balance = starting_balance
  end

  def balance #instance getter method
     @balance #instance variable visible only to this object
  end

  def balance=(new_amount)
     @balance = new_amount
  end

  def deposit(amount)
     @balance+=amount
  end

  @@bank_name= "MyBank.com" # class (static) variable

  # A class method
  def self.bank_name
     @@bank_name
  end
  # or: def SavingsAccount.bank_name : @@bank_name : end

end

I want to understand the code snippets in bold. What do they do? what is the difference between a setter and initialize method.
If I had an object test=Account.new() and why is test(30) giving an error. Isn’t that suppose to call the setter method with parameter 30 and set the balance?

  • 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-08T22:29:39+00:00Added an answer on June 8, 2026 at 10:29 pm

    initialize is the method that is called on the newly created object when you do Account.new or Account.new(my_starting_balance). In the first case initialize would be called with the default value 0 for starting_balance and in the second with my_starting_balance.

    The setter method balance= is called when you do my_account.balance = some_value where my_account is an instance of the class Account. So if you have the following code, initialize will be called on line 1 (with 0 as its argument) and balance= on line 2 (with 23) as its argument:

    my_account = Account.new
    my_account.balance = 23
    

    Of course in this case I could just as well write the following and not use the setter method at all:

    my_account = Account.new(23)
    

    However that doesn’t always work because some times you might want to change the value of balance after the object has already been created.

    If I had an object test=Account.new() and why is test(30) giving an error.

    Because test(30) means “call the method test with the argument 30″ and there is no method called test in your code.


    Regarding the second bolded part of your code: As the comments indicate, it sets a class variable named @@bank_name and defines a class method that returns that variable’s value.

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

Sidebar

Related Questions

I have this class: class Account attr_accessor :balance def initialize(balance) @balance = balance end
Here is what I'm attempting to do: class Account < ActiveRecord::Base def name end
This is my class: class Account(models.Model): name = models.CharField(max_length=255) def __unicode__(self): return '{0}'.format(self.name) class
I have the following module/class: module Pigeons class FedEx attr_accessor :signature_name def initialize(account) self.account
I have a model, for example : class Account < ActiveRecord::Base before_create :build_dependencies def
I have a class called Account public class Account { public double balance =
Say I have this class: public class Account { public int AccountID { get;
class Account < ActiveRecord::Base after_update :give_user_credit, :on => :update def give_user_credit credit = User.current_user.credit
I have this: class OrderForm(ModelForm): class Meta: model = Order exclude = ('number',) def
I have a domain model that looks like this: case class Account(id: Int, name:

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.