Forum: Ruby on Rails Conditional statement style

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Stephen B. (Guest)
on 2006-05-03 21:07
(Received via mailing list)
This is a bit of a nuby question methinks but i've had a play/search
around and it's just bugging me now :0)

Say I have a conditional statement, testing a variable against a number
of values.  The first way i did this is like so:

if myvar != "" and myvar != 0 and myvar != nil
   # .. code
end

This seems a bit messy - is there neater way of doing this? I thought
of:

if myvar != ("" || 0 || nil)

but ||, as i understand it, is a conditional assigner that causes the
current value the fall through to the preceeding value, if the
preceeding value is nil.  I'm sure that sentence made sense in my head
before i wrote it...

Anyway - any ideas would be appreciated,

Cheers,

Steve
Daniel -. (Guest)
on 2006-05-03 21:07
(Received via mailing list)
Does the or statement work exactly the same as the || ?

if myvar != ("" or 0 or nil)
Dan Venkitachalam (Guest)
on 2006-05-03 21:07
(Received via mailing list)
You could try:

unless ["", 0, nil].include?(myvar)
...
end
This topic is locked and can not be replied to.