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
|