Banking on Ruby

I have finished the Banking on Ruby code but all i get when I run it is “Access denied: incorrect PIN.
Access denied: incorrect PIN.” Can someone explain what’s wrong?

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

  end
  public
  def display_balance(pin_number)
    if pin_number == @pin
      puts "Balance: $#{@balance}."
    else
      puts pin_error
    end
  end
  
  def withdraw(pin_number,amount)
    if pin_number == @pin
      @balance -= amount
      puts "Withdrew #{amount}.New balance: $#{@balance}."
    else
      puts pin_error
    end
  end
  def deposit(pin_number, amount)
    if pin_number == @pin
      @balance += amount
      puts "Deposit #{amount}. New balance: $#{@balance}"
      else 
      puts pin_error
    end
  end
  private 
  def pin
    @pin =1234
  end
  def pin_error
    return "Access denied: incorrect PIN."
  end 
end
checking_account=Account.new("WF checking", 1_000)
checking_account.deposit(1234, 100)
checking_account.display_balance(1234)

This is not defined in present scope since it is returned from the pin method.

if pin_number == pin
4 Likes

Thank you so very much for resolving this. Just a quick follow up question, if I may?
Why is it that the lesson teaches to use @pin =1234 as in below snippet.
private
def pin
@pin =1234
end

1 Like

Because it is private we should not be able to access it without referring to the method. (If we declared it as public, a user could access it on the instance, which would not be a good thing, one thinks.) By having in a private method, only the instance methods can access it.

To test this theory, see if you can access the pin method on your instance object.

puts checking_account.pin
  • parenthesized statement needs corroboration or clarification.
4 Likes

Perfect, thanks again!

1 Like