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

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

Kind regards

robert

···

On Wed, Nov 19, 2014 at 3:07 PM, Roelof Wobben <r.wobben@home.nl> wrote:

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

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

Hi,

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.

···

In message "Re: what is wrong with this if" on Wed, 19 Nov 2014 15:07:47 +0100, Roelof Wobben <r.wobben@home.nl> writes:

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 Wobben <r.wobben@home.nl> 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

Robert Klemme schreef op 19-11-2014 15:26:

···

On Wed, Nov 19, 2014 at 3:07 PM, Roelof Wobben <r.wobben@home.nl> wrote:

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

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

Kind regards

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

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

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 Wobben <r.wobben@home.nl> >             wrote:
              pin_number

== @pin ? puts(“Balance: $#{@balance}.”) : pin_error
Yukihiro
Matsumoto schreef op 19-11-2014 16:03:

          Sorry, it works,



          My fault is that I made it a private function.



              Roelof

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 Matsumoto <matz@ruby.or.jp>
To: ruby-talk@ruby-lang.org
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 Wobben <r.wobben@home.nl> 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\(&quot;Balance: $\#\{@balance\}\.&quot;\) : pin\_error

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

if pin\_number == @pin
  puts\(&quot;Balance: $\#\{@balance\}\.&quot;\)
else
  pin\_error
end

                        matz\.

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

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

Regard's
Sumit Pahuja

···

On Wed, Nov 19, 2014 at 8:39 PM, Roelof Wobben <r.wobben@home.nl> 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

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

robert

···

On Wed, Nov 19, 2014 at 3:30 PM, Roelof Wobben <r.wobben@home.nl> 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

--
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/

Hello,

Try this, it will work.

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

Regard's
Sumit Pahuja

···

On Wed, Nov 19, 2014 at 8:00 PM, Roelof Wobben <r.wobben@home.nl> wrote:

Robert Klemme schreef op 19-11-2014 15:26:

On Wed, Nov 19, 2014 at 3:07 PM, Roelof Wobben <r.wobben@home.nl> wrote:

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

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

Kind regards

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

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