cannot return negative value ?

Discussion in 'Ruby' started by Josselin, Jan 15, 2007.

  1. Josselin

    Josselin Guest

    I got the following error :

    parse error, unexpected tUMINUS_NUM

    userCredential.flagged? ? return -1 : return 1

    what's wrong ?

    I tried also

    userCredential.flagged? ? return (-1) : return 1

    same problem.....
    Josselin, Jan 15, 2007
    #1
    1. Advertising

  2. On 15/01/07, Josselin <> wrote:
    > I got the following error :
    >
    > parse error, unexpected tUMINUS_NUM
    >
    > userCredential.flagged? ? return -1 : return 1
    >
    > what's wrong ?
    >
    > I tried also
    >
    > userCredential.flagged? ? return (-1) : return 1
    >
    > same problem.....


    This seems to work:

    return userCredentials.flagged? ? -1 : 1

    Farrel
    Farrel Lifson, Jan 15, 2007
    #2
    1. Advertising

  3. Josselin

    Peter Szinek Guest

    Josselin wrote:
    > I got the following error :
    >
    > parse error, unexpected tUMINUS_NUM
    >
    > userCredential.flagged? ? return -1 : return 1

    You mean

    return userCredential.flagged? -1 : 1

    ?

    I am not sure in Ruby, but if it's the same than in C or anywhere else,
    the ternary operator evaluates to a value, and you can not really
    execute things like this inside...

    Cheers,
    Peter

    __
    http://www.rubyrailways,com
    Peter Szinek, Jan 15, 2007
    #3
  4. Josselin

    Josselin Guest

    On 2007-01-15 12:43:21 +0100, Peter Szinek <> said:

    > Josselin wrote:
    >> I got the following error :
    >>
    >> parse error, unexpected tUMINUS_NUM
    >>
    >> userCredential.flagged? ? return -1 : return 1

    > You mean
    >
    > return userCredential.flagged? -1 : 1
    >
    > ?
    >
    > I am not sure in Ruby, but if it's the same than in C or anywhere else,
    > the ternary operator evaluates to a value, and you can not really
    > execute things like this inside...
    >
    > Cheers,
    > Peter
    >
    > __
    > http://www.rubyrailways,com


    thanks a lot first time I was using this kind of return... ;-))
    Josselin, Jan 15, 2007
    #4
  5. Josselin

    Josselin Guest

    On 2007-01-15 12:41:09 +0100, "Farrel Lifson" <> said:

    > On 15/01/07, Josselin <> wrote:
    >> I got the following error :
    >>
    >> parse error, unexpected tUMINUS_NUM
    >>
    >> userCredential.flagged? ? return -1 : return 1
    >>
    >> what's wrong ?
    >>
    >> I tried also
    >>
    >> userCredential.flagged? ? return (-1) : return 1
    >>
    >> same problem.....

    >
    > This seems to work:
    >
    > return userCredentials.flagged? ? -1 : 1
    >
    > Farrel


    hanks a lot first time I was using this kind of return... ;-))
    Josselin, Jan 15, 2007
    #5
  6. On 15/01/07, Peter Szinek <> wrote:
    > Josselin wrote:
    > > I got the following error :
    > >
    > > parse error, unexpected tUMINUS_NUM
    > >
    > > userCredential.flagged? ? return -1 : return 1

    > You mean
    >
    > return userCredential.flagged? -1 : 1
    >
    > ?
    >
    > I am not sure in Ruby, but if it's the same than in C or anywhere else,
    > the ternary operator evaluates to a value, and you can not really
    > execute things like this inside...
    >
    > Cheers,
    > Peter


    This seems to work though

    userCredentials.flagged? ? (return -1) : (return 1)

    Farrel
    Farrel Lifson, Jan 15, 2007
    #6
  7. Hi,

    Am Montag, 15. Jan 2007, 20:30:08 +0900 schrieb Josselin:
    > I got the following error :
    >
    > parse error, unexpected tUMINUS_NUM
    >
    > userCredential.flagged? ? return -1 : return 1
    >
    > what's wrong ?


    when saying

    userCredential.flagged? ? (return -1) : (return 1)

    or even

    userCredential.flagged? ? begin return -1 end : begin return 1 end

    you will probably see that the return statement is definitely in the
    wrong place.

    Bertram


    --
    Bertram Scharpf
    Stuttgart, Deutschland/Germany
    http://www.bertram-scharpf.de
    Bertram Scharpf, Jan 15, 2007
    #7
  8. Hi,

    At Mon, 15 Jan 2007 20:30:08 +0900,
    Josselin wrote in [ruby-talk:234078]:
    > I got the following error :
    >
    > parse error, unexpected tUMINUS_NUM
    >
    > userCredential.flagged? ? return -1 : return 1
    >
    > what's wrong ?


    Because `return' is a statement. In Ruby, a "statement" is
    defined as an expression which has lower precedence and can't
    appear directly in another expression.

    --
    Nobu Nakada
    Nobuyoshi Nakada, Jan 15, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. prem_eda
    Replies:
    5
    Views:
    7,854
    Pieter Hulshoff
    Oct 11, 2004
  2. ABC
    Replies:
    1
    Views:
    3,184
    Bruce Barker
    Nov 17, 2005
  3. Charles
    Replies:
    6
    Views:
    13,952
  4. Greenhorn
    Replies:
    15
    Views:
    815
    Keith Thompson
    Mar 6, 2005
  5. Replies:
    8
    Views:
    291
Loading...

Share This Page