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
Does the or statement work exactly the same as the || ? if myvar != ("" or 0 or nil) On 5/3/06, Stephen Bartholomew <sb@2404.co.uk> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060503/907b0547/attachment.html
You could try: unless ["", 0, nil].include?(myvar) ... end On 5/3/06, Stephen Bartholomew <sb@2404.co.uk> wrote:> > 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 > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Dan Venkitachalam expandingbrain.com -------------- next part -------------- An HTML attachment was scrubbed... URL: http://wrath.rubyonrails.org/pipermail/rails/attachments/20060503/0f26b5b2/attachment.html