Possible to make compound if statements

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.

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.)

Don’t repeat the if. It’s like most languages.

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

You can also use || as in C. There are some differences, but
in this case they’d work identically.

Hal

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

if field[0] == “AIX” or field[2] =~ /ldap/

puts “BLAH”
end

-austin

···


austin ziegler * austin@halostatue.ca * Toronto, ON, Canada
software designer * pragmatic programmer * 2003.09.03
* 00.48.16

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

···

On Wed, 3 Sep 2003 12:54:04 +0900 Kurt Euler keuler@portal.com 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.)