Why does pin_number == pin instead of @pin? when checking for match in if statement


#1



https://www.codecademy.com/en/courses/ruby-beginner-en-32cN3/0/6?curriculum_id=5059f8619189a5000201fbcb

whenever the pin number is checked, its always compared to pin instead of @pin. why?


  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


#2

Hello @cssrunner97411,

In Ruby, there are four types of variables:

  • Global variables (starting with $)
  • Instance variables (starting with @)
  • Class variables (starting with @@)
  • Local variables (starting with a-z or _)

In this case @pin is an instance variable, because the pin is unique to every instance of the class Account. But then, after initialisation, pin doesn't need to be referred to as @pin anymore, since the class automatically understands what pin is.

Hope this clears your doubts...

Regards,
Barry Allen(@bartholomewallen)