Banking of ruby 4/7 displaying the balance issue

The exercise asks you to do the following:

Blockquote
Define a public display_balance method to your Account class. It should take a single parameter, pin_number. The body of your method should check whether the pin_number is equal to pin (the result of calling the private pin method). If it is, display_balance should puts “Balance: $#{@balance}.” Otherwise (else), it should puts pin_error (the pin_error message)."

Here is my code. I am not sure why it is that when I run the code, I get an output of Balance = $500.

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)
    if pin_number = pin
     puts "Balance: $#{@balance}."
   else
      puts pin_error
    end
  end

end

But the exercise has this code for display_balance method

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

when I use the same code and run it then I don’t get any output, which I am assuming is how it should be. Why does my code give an output? Am I doing something wrong?

Thanks

What did you enter to create an instance; can you show us those lines?

That is a ternary expression statement which is covered at some point in the control flow section, or perhaps in the refactoring unit.

I have given my code exactly as its shown in my question. There aren’t nay extra lines.

What is the error message?

there is no error message. It lets me proceed to the next level. It’s just that when I run the code I get a value of Balance = $500. Where is that value coming from? When I replace the code I wrote with the existing code then I don’t get an output. Why?

Also in the next exercise 5/7 Making a withdrawal. I am getting the same this. This time I compared my code to the existing code and it was the same. But when I run it, I get an output of “Withdrew = 0 and New Balance = $500”

I am just curious where these values are coming from?

If it is not you who is creating the new instance then it must be the SCT (Submission Correctness Test) that is doing it to test your code. That would explain the amount.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.