Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Possible to make compound if statements...

Reply
Thread Tools

Possible to make compound if statements...

 
 
Kurt Euler
Guest
Posts: n/a
 
      09-03-2003
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.

 
Reply With Quote
 
 
 
 
Jason Creighton
Guest
Posts: n/a
 
      09-03-2003
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Mark up compound noun so that search engines see two words Greg N. HTML 20 02-15-2006 05:52 PM
RE: Compound Control event not firing, but only when it's in a library =?Utf-8?B?TWlrZUw=?= ASP .Net 0 11-19-2004 04:45 AM
JAI problem of creating compound image from slices Apc Java 1 06-02-2004 10:10 PM
Thermal compound or not buffy Computer Support 11 10-14-2003 03:37 AM
CMR/CMP and Compound Primary Key Damir Mikoc Java 1 07-04-2003 03:27 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57