This forum is now read-only. Please use our new forums! Go to forums
4.Displaying the Balance - code check?
Hi, can someone review my code?
class Account
attr_reader :name, :balance
def initialize(name, balance=100)
@name = name
@balance = balance
end
def display_balance(pin_number)
puts pin_number == pin ? "Balance: $#{@balance}." : pin_error
end
private
def pin(pin)
@pin = 1234
end
private
def pin_error
"Access denied: inccorect PIN."
end
end
The message I get is “Oops, try again. Did you give your display balance a single pin number parameter?”
Answer 51421760f8bb245a05000c4f
The Ruby interpreter chokes on the method definition of display_balance
because inside it you are trying to call pin
without an argument, but def pin(pin)
expects one. Unlike JavaScript, Ruby is very strict about method signatures (the number of arguments you can call a method with).
But since the submission correctness test also expects your pin
method to have no arguments, don’t change anything inside display_balance
. Rather, just remove (pin)
from the definition of the pin
method, and you’ll be fine.
Another comment:
You don’t need several private
keywords: one is enough. It tells Ruby that every method that follows it will be private (unless a public
keyword opens another section of public methods):
class MyClass
def initialize; end # `initialize` is always public
private
def private_method_1; end
def private_method_2; end
def private_method_3; end
public
def i_am_public; end
end
Popular free courses
- Free Course
Learn SQL
In this SQL course, you'll learn how to manage large datasets and analyze real data using the standard data management language.Beginner friendly,4 LessonsLanguage Fluency - Free Course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner friendly,11 LessonsLanguage Fluency - Free Course
Learn HTML
Start at the beginning by learning HTML basics — an important foundation for building and editing web pages.Beginner friendly,6 LessonsLanguage Fluency
1 comments
Thanks for the explanation, much appreciated!