What is wrong with this if

Hello,

I have this method :

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

But now I see these errors :

(ruby):23: syntax error, unexpected tSTRING_BEG, expecting keyword_do or
‘{’ or ‘(’
pin_number == @pin ? puts “Balance: $#{@balance}.” : pin_error
^
(ruby):23: syntax error, unexpected ‘:’, expecting keyword_end
pin_number == @pin ? puts “Balance: $#{@balance}.” : pin_error

Roelof

On Wed, Nov 19, 2014 at 3:07 PM, Roelof W. [email protected] wrote:

(ruby):23: syntax error, unexpected tSTRING_BEG, expecting keyword_do or ‘{’
or ‘(’
pin_number == @pin ? puts “Balance: $#{@balance}.” : pin_error
^
(ruby):23: syntax error, unexpected ‘:’, expecting keyword_end
pin_number == @pin ? puts “Balance: $#{@balance}.” : pin_error

I think you got a syntax error because there are no brackets around
arguments to puts.

Kind regards

robert

Robert K. schreef op 19-11-2014 15:26:

robert

Sorry I try this and both do not work :

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

Roelof

Hello,

Try this, it will work.

pin_number == @pin ? puts(“Balance: $#{@balance}.”) : pin_error

Regard’s
Sumit Pahuja

Hi,

In message “Re: what is wrong with this if”
on Wed, 19 Nov 2014 15:07:47 +0100, Roelof W. [email protected]
writes:

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

Only expression is allowed for then-part of ternary conditionals.
So it should be

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

But I think the following is more intuitive, unless you’re trying to
reduce code lines.

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

          matz.
Sumit Pahuja schreef op 19-11-2014 15:38:
pin_number == @pin ? puts("Balance: $#{@balance}.") : pin_error
Nope?? , then I see this error :

Oops, try again. Did you add a public display_balance method to your Account class

On Wed, Nov 19, 2014 at 3:30 PM, Roelof W. [email protected] wrote:

Sorry I try this and both do not work :

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

It’s a method call. You got the wrong brackets.

robert

Nice that your error resolved, even this will also work.

pin_number == @pin ? ( puts “Balance: $#{@balance}.” ) : pin_error

Regard’s
Sumit Pahuja

Yukihiro M. schreef op 19-11-2014 16:03:

pin_number == @pin ? puts(“Balance: $#{@balance}.”) : pin_error

Sorry, it works,

My fault is that I made it a private function.

Roelof

I’m not sure any of the previous answers will help you learn ruby,
hopefully this is a better explanation to why this error occurred.

Your original statement:
pin_number == @pin ? puts “Balance: $#{@balance}.” : pin_error

the ruby interpreter thinks you are doing this
if pin_number == @pin

puts “Balance: $#{balance}” : pin_error

else

since you didn’t include the parentheses it tries to pass the remaining
parts of the short-hand conditional into the puts function

if you try calling:
puts “Balance: $#{@balance}” : pin_error

you will see a similar error message.

By putting the parentheses around “Balance: $#{@balance}” you are
telling
the ruby interpreter that you only want that argument passed into the
puts
function.

Hopefully that sheds some more light on the error.

-Cameron

I like the second version
if pin_number == @pin
puts(“Balance: $#{@balance}.”)
else
pin_error
end
because in the future if we want to add more instructions it will be
easily to insert it.
I think of the code as 5 dimensions image(1) imperative (instruction
under instruction - do this then do that)(2) one line expression (long
horizontal)(3) nested (structures inside structures)(4) pointers (jump
up/down or forward/backward)(5) Abstraction (Files, Modules, Classes,
Functions,…etc)
selecting the right dimension for your code is an art
Also some guidelines would help in the process

Greetings,Mahmoud

  From: Yukihiro M. <[email protected]>

To: [email protected]
Sent: Wednesday, November 19, 2014 6:03 PM
Subject: Re: what is wrong with this if

Hi,

In message “Re: what is wrong with this if”
on Wed, 19 Nov 2014 15:07:47 +0100, Roelof W. [email protected]
writes:

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

Only expression is allowed for then-part of ternary conditionals.
So it should be

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

But I think the following is more intuitive, unless you’re trying to
reduce code lines.

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

                        matz.
Thanks for the explanation.

Roelof


Cameron Crockett schreef op 19-11-2014 16:40:
I'm not sure any of the previous answers will help you learn ruby, hopefully this is a better explanation to why this error occurred.

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

the ruby interpreter thinks you are doing this
?? if pin_number == @pin
puts "Balance: $#{balance}" : pin_error
?? else

since you didn't include the parentheses it tries to pass the remaining parts of the short-hand conditional into the puts function

if you try calling:
?? ?? ??puts "Balance: $#{@balance}" : pin_error

you will see a similar error message.??

By putting the parentheses around "Balance: $#{@balance}" you are telling the ruby interpreter that you only want that argument passed into the puts function.

Hopefully that sheds some more light on the error.

-Cameron

On Wed, Nov 19, 2014 at 9:09 AM, Roelof W. <[email protected]> wrote:
Yukihiro Matsumoto schreef op 19-11-2014 16:03:
pin_number == @pin ? puts("Balance: $#{@balance}.") : pin_error

Sorry, it works,

My fault is that I made it a private function.

Roelof