FAQ: Banking on Ruby - Well Done!

So I am still confused on why it works when display_balance has
if pin_number == pin

and if all three are pin_number == @pin
it doesn’t work ?

All three methods that check the pin_number should be accessing it through the pin method for consistency.

But somehow leaving the two lower methods with if pin_number == @pin

when the first method has if pin_number == pin – works and all three methods run as if given the correct pin

The purpose of having a method to get @pin is to reduce exposure of the instance variable. The only place we should see @pin is in that method. This gives genuine consistency.

In case I may have called @pin a private variable, belay that. pin is a private method. @pin is not private. The fact that the method is private means it can only be invoked by methods, not instances.

This topic is one that needs extra literature to support it. One does not wish to mislead, but presently I lack the time to source out some good reading. Search for all the Ruby docs you can find that are officially sanctioned, including Wikis or Wikibooks.

Read the specs and study the demos. It’s good that you are still a little confused. That you are sticking with it will go a long way to reinforcing the concepts of class vs instance variables, and private vs public methods. Don’t leave this topic until you are really comfortable else it may trip you up down the road. Now is the time to take this opportunity to dig in for info, facts, demos, interpretations, &c.

1 Like


Hi guys ! I need your help please.

Here my code :

class Account
  attr_reader :name, :balance
  def initialize(name, balance=100)
    @name = name
    @balance = balance

  def display_balance
    puts "Hello #{name} ! Your balance is $ #{balance}."

  def pin
    @pin = 1234

  def pin_check(pin_number)
    if pin_number == @pin
      puts "Access authorized: pending transaction"
    else puts "Access denied: wrong PIN"

checking_account = Account.new("Saitama", 20_000)

What I’m trying to do is to automate the “pin check” in the other variables. My problem here is with the pin_check argument : of course, the program is expecting an argument from this method but on my last line of code, I don’t know how to give it the 1234 argument expected… As you can see, I’m giving it to the display_balance variable. Is there any way to write correctly this last line of code to link display_balance and pin_check so as to giving the good argument to pin_check ?

I hope my question makes sense ! Thanks for your help !

pin_check takes one positional argument so the call should have one.

The instance attribute, @pin is defined in the pin method.

if pin_number == pin

Something that arises as an issue is the fact that nothing prevents the balance from displaying (assuming it does) regardless the outcome of the pin check.

Hi mtf !

Thanks for your answer.

Ok that’s what I was thinking ! But how can I write that ? I can’t just write this, correct ?

  def display_balance
    puts "Hello #{name} ! Your balance is $ #{balance}."

I need to write it when I’m doing my checking_account I guess. But how can I write that ? I know the following code won’t work but is something like this could work ?


Yes you’re right ! I have to figure it out and find out how to do that :slight_smile:

1 Like

display_balance will need to take an argument so it has the pin_number to pass along.

When we look at the method call, it contains one argument, so this method needs to receive that argument with a parameter.

def display_balance(pin_number)

1 Like