Displaying the Balance


#1



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


the relevant part of this code is the display_balance method, but i ve included the code in its entirety. It is exactly the same as in the original code in part one as far as i see but doesnt work and gives the following error.

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

(ruby):19: syntax error, unexpected ':', expecting keyword_end
puts pin_number==pin? "Balance: $#{@balance}.": pin_error


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
        "Access denied: incorrect PIN."
    end
    
    public
    def display_balance(pin_number)
        puts pin_number==pin? "Balance: $#{@balance}." : pin_error
    end
end


#2

Have you tried with space before ? ?


#3

thanks, that worked. i thought white space didnt' count in ruby. are there situations when it does count like this one?


#4

One that comes to mind is .nil?.

if movies[title.to_sym].nil?

In other words, if the title does not exist in the hash, then ...

Another would be .is_a?

if n.is_a? (Object)

Couple more that come to mind,

.instance_of?
.kind_of?

What you have above is a ternary expression, so the ? is a stand alone operator.


#5

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