Possible to make compound if statements...

K

Kurt Euler

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

Jason Creighton

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top