Banking on Ruby 6. Opening an Account


#1

Hi I need help on the Banking on Ruby Opening Account lesson I'm getting the following error: undefined local variable or method `public' for #<Context::Account:0x000000015b1660 @name="eva", @balance=1000>

This is my code below:
class Account
attr_reader :name, :balance
def initialize(name, balance=100)
@name = name
@balance = balance

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

    public
    def withdraw(pin_number, amount)
    if pin_number == pin
       @balance -= amount
       puts "Withdrew #{amount}."
   else 
       puts pin_error 
   end
 end

    private
    def pin
        @pin = 1234; end
    def pin_error
        return "Access denied: incorrect PIN."; end
end

end

checking_account = Account.new("eva", 1_000)

Also when I add an end to the class Account it gives an error because I thought perhaps I needed to add end after looking at original example in Lesson 1 of this section but that gives the error of : (ruby):26: syntax error, unexpected keyword_end, expecting $end


#2

How is public supposed to be used, where does it go? How does one call a method, which it claims is what you're doing with public?

I suggest cleaning up your formatting and then having a look at examples of using public and then putting it in an appropriate location. (Messy formatting makes it difficult to tell what's what)
In particular what's less than ideally formatted is your end-keywords, and some of them aren't just formatted badly, they're in the wrong place.


#3

Thanks I figured it out I didn't realize I added an extra end to the pin_error method. Once that was removed it worked.


#4

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