Possible to make compound if statements...

Discussion in 'Ruby' started by Kurt Euler, Sep 3, 2003.

  1. Kurt Euler

    Kurt Euler Guest

    All-

    Is it possible to make compound if statements in Ruby, something like:

    if field[0] == "AIX" or if field[2] =~ /ldap/
    puts "BLAH"
    end

    (but different, of course, since the above doesn't work.)

    Thanks.
     
    Kurt Euler, Sep 3, 2003
    #1
    1. Advertising

  2. On Wed, 3 Sep 2003 12:54:04 +0900
    Kurt Euler <> wrote:

    > All-
    >
    > Is it possible to make compound if statements in Ruby, something like:
    >
    > if field[0] == "AIX" or if field[2] =~ /ldap/
    > puts "BLAH"
    > end
    >
    > (but different, of course, since the above doesn't work.)


    Wrong syntax, use 'or'

    if field[0] == "AIZ" or field[2] =~ /ldap
    puts "BLAH"
    end

    Now here's the interesting bit: The syntax you used was almost valid!
    Look here:

    ~/prog/ruby$ cat weird-if.rb
    a,b,c = 1,2,3

    if a == 1 and if b == 2
    puts "b == 2"
    c > 3
    end
    puts "BLAH!"
    end

    puts "Different way of saying the same thing:"

    if a == 1 and (if b == 2 then puts "b == 2"; c > 3; end)
    puts "BLAH!"
    end
    ~/prog/ruby$ ruby weird-if.rb
    b == 2
    Different way of saying the same thing:
    b == 2
    ~/prog/ruby$

    So that construct is the same as:

    if true and false
    puts "BLAH!"
    end

    where:

    true: a == 1
    false: The result of evaling the 'if' statement is the last expression
    evalulated, that is, c > 3

    So obviously, "BLAH!" isn't printed.

    Jason Creighton
     
    Jason Creighton, Sep 3, 2003
    #2
    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. Neil Zanella
    Replies:
    8
    Views:
    1,195
    mfmehdi
    Oct 20, 2006
  2. Replies:
    3
    Views:
    870
    Steve Holden
    Sep 16, 2003
  3. Fredrik Tolf

    Compound statements in expressions

    Fredrik Tolf, Sep 30, 2004, in forum: C Programming
    Replies:
    18
    Views:
    515
    Fredrik Tolf
    Sep 30, 2004
  4. Neil Cerutti

    Nested compound statements.

    Neil Cerutti, Feb 1, 2008, in forum: Python
    Replies:
    0
    Views:
    333
    Neil Cerutti
    Feb 1, 2008
  5. Randy Kramer
    Replies:
    11
    Views:
    372
    Randy Kramer
    Oct 19, 2007
Loading...

Share This Page