FAQ: Banking on Ruby - Displaying the Balance

This community-built FAQ covers the “Displaying the Balance” 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 Displaying the Balance

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!

Why my balance shows as 500? Where did that 500 come from?

3 Likes

Hello, @method0228691432.

Welcome to the forums.

You’ll have to post your code for anyone to hazard a guess.

Hello! I had the same issue:

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

also shows Balance: $500.

1 Like

Also, more than the question above (that I think is probably the editor getting information from the first code maybe?), I am stuck on this:

In the first topic, this is the code:

In the solution, this is the code:

I understand that the main topic uses ternary instead of if-else.

What I don’t get is why the method checks a variable pin in the first, and in the solution it checks the instance @pin .

Also, why should we puts the instance @balance instead of balance itself?

Thanks in advance!!

Hello,

Is the $ sign in string literal "Balance: $#{@balance}." needed, and if needed, why?
When I run the code without the $ signs, it works fine. If code is trying to use $ as global variable, shouldn’t it be "#{$balance}"? But it’s not a global variable, so where is this $ coming from?

Thank you for your help in advance.

In this case, the $ (dollar sign) is simply a printed character to denote dollars in the output. It has no significance regarding the scope of the variable or otherwise.

5 Likes

Of course it is! Thank you so much :slight_smile:

1 Like

My code also shows “Balance: $500”, but clears to an empty console when the code reruns. I, too, am curious as to where/how this number is generating.

while I dont know why it is showing 500 for you, I noticed that your

public
def display_balance(pin_number)
if pin_number == pin
puts “Balance: $#{@balance}.”
else puts pin_error
end

was under the private methods. And according to the exercise hints

Something important to note: you can explicitly declare your public methods public, or you can omit public and your methods will be public by default. However! If you don’t use public, you need to put your public methods before the private keyword, since private affects every method after it appears. If you put your public methods below private and don’t label them public, they’ll be private, too!

that might affect it?

Is there something i’m missing?

Old post but it’s something worth bearing in mind for anyone…should be pin_number and not pin number. Watch out for syntax errors, including typos, missing parentheses, and similar. :slight_smile: