End error's and why that line of code?


#1

Please help, experts!
I do not understand why I get the below error message, nor that piece of code further down

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

ruby):19: syntax error, unexpected $end, expecting keyword_end

class Account
    attr_reader :name, :balance
    def initialize(name, balance=100)
    @name = name
    @balance = balance
end
    private
    def pin
        @pin = 1234 
end
    def pin_error 
        return "Access denied: incorrect PIN." 
end
    public
    def display_balance(pin_number)
           if pin == pin_number 
               puts "Balance: $#{@balance}."
           else puts pin_error
end
end

nor to I understand what this piece of code does

puts pin_number == pin ? "Balance: $#{@balance}." : pin_error

No doubt my code is missing the above line but I cannot understand why, according to the instructions all that is needed is

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

any help VERY welcome!
cheers


#2

Hi this part it's missing the end of the if and else statement

    def display_balance(pin_number)
        if pin == pin_number 
            puts "Balance: $#{@balance}."
        else 
            puts pin_error 
        end <-- #add this end
    end

#3

Once again thanks wizmarco,

that worked. I forgot that the else statement needs its own farewell.


#4

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

Any idea what this error means: (ruby):24: syntax error, unexpected $undefined
...nce #${@balance}."
... ^
(ruby):24: unterminated string meets end of file

I know its referring to the balance line but don't understand what is wrong with it.

my_code:

class Account
    attr_reader :name
    attr_reader :balance
    def initialize(name, balance=100)
    @name = name
    @balance = balance
end
    private
    def pin
        @pin = 1234 ; end
    def pin_error 
        return "Access denied: incorrect PIN."
end
    public
    def display_balance(pin_number)
        if pin == pin_number
        puts "Balance: $#{@balance}."
        else puts pin_error
        end
        
        public
        def withdraw(pin_numer, amount)
            if pin == pin_number
            @balance -= amount 
            puts "Withdrew #{amount}. New balance #${@balance}."
           else puts pin_error
           end
end
end

#5

I have even added the other two 'ends'.

one to close the if and one for the else, finally I close the class with one end.


#6

The string of the lesson is

puts "Withdrew #{amount}. New balance: $#{@balance}."

and yours

puts "Withdrew #{amount}. New balance #${@balance}."

the diff is that you wrote #${@balance} instead of $#{@balance} then here

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

it's missing the end
of the method


#7

ow dear, I think I should call it a day.

Can't see the tree for the forest, kind of thing.

Thanks wizmarco...


#8

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