FAQ: Banking on Ruby - Opening an Account

This community-built FAQ covers the “Opening an Account” exercise from the lesson “Banking on Ruby”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Ruby

FAQs on the exercise Opening an Account

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Can anyone help me figure out why I am getting the error code " Did you create a variable called checking_account?"

class Account
attr_reader :name
attr_reader :balance
def initialize(name,balance=100)
@name = name
@balance = balance
end
private
def pin
@pin = 1234
end
private
def pin_error
return “Access denied: incorrect PIN.”
end
public
def display_balance(pin_number)
@pin_number = pin_number
puts “Balance: $#{@balance}.”
else
puts “pin_error”
end

public
def withdraw(pin_number,amount)
puts “withdrew #{amount}. New balance: $#{@balance}.”
else
puts “pin_error”
end
checking_account = Account.new(“Brian”, 7500)
end

you’re also missing if statements, at least based on what you pasted… also you need to create a new variable outside of the class

class Account
attr_reader :name
attr_reader :balance
def initialize(name,balance=100)
@name = name
@balance = balance
end
public
def display_balance(pin_number)
puts (pin_number == pin) ? “Balance: #{@balance}” : pin_error
end
def withdraw(pin_number,amount)
if (pin_number == pin)
@balance -= amount
puts “Withdrew #{amount}. New balance: $#{@balance}.”
else
puts pin_error
end
end
private
def pin
@pin = 1234
end
def pin_error
“Access denied: incorrect PIN.”
end
end
checking_account = Account.new(“David”,1_000_000_000)

1 Like

i had a question though… should we be removing

attr_reader :balance
because it allows someone to pass a .balance on their account variable bypassing the pin req from display_balance

2 Likes

checking_account should be introduced outside of the class definition, after the last end.

Hey there! I’m trying to test the program but it says that my pin is incorrect. Could you please help?

class Account
  attr_reader :name, :balance

  def initialize(name, balance=100)
    @name = name
    @balance = balance
  end

  private
  def pin
    @pin = 1234
  end

  def pin_error
    "Access denied: incorrect PIN."
  end

  public
  def display_balance(pin_number)
    pin_number == @pin ? "Balance: $#{@balance}." : pin_error
  end

  def withdraw(pin_number, amount)
    if pin_number == @pin 
      @balance =-amount 
      puts "Withdrew #{amount}" 
    else 
      pin_error
    end
  end
end

checking_account = Account.new("S", 500)
puts checking_account.withdraw(1234, 200)
puts checking_account.display_balance(1234)

Have a look at this post:

Basically, you create a new Account which is fine. Then you try to withdraw some money from this account. In the withdraw method, you have the condition if pin_number == @pin, but the thing is that the instance variable @pin hasn’t been initialized yet. So, the condition evaluates as false.
If you use the condition if pin_number == pin, then the issue can be avoided. See the linked post for more detail. If still confused about something, share your thoughts.

1 Like

Ah, I see, thanks a lot for the explanation!

1 Like