I seem to be having trouble using a conditional OR statement in my code.
Hopefully someone can point out my error.
In one of my controllers I have the following statement:
if @thisVar == (0||1||6)
dosomething
end
But this won’t work. Instead, I end up having to do this:
if @thisVar == 0
dosomething
elsif @thisVar == 1
dosomething
elsif @thisVar == 6
dosomething
end
Which is of course, redundant code, since the same “dosomething” gets
typed 3 times.
I have had this same problem in .rhtml code, when I use something like:
<% if @thisVar == (0||1||6) %><%= dosomething %><% end %>
In looking at the Ruby documentation for conditional statements, I have
seen code like this with the explanation that it works. What am I doing
wrong?
Nathan M.
Operations Director
Northeast Region
Pilgrim IT, LLC
NORTHEAST OFFICE
1 Short Street
Northampton, MA 01060
TEL 866.434.4976
FAX 413.587.0572
MIDWEST OFFICE
1815 Brownsboro Road
Louisville, KY 40206
TEL 502.721.7939
FAX 502.721.7940
NOTICE: This email and any attachments are intended only for the
addressee and may contain information that is confidential and/or
legally privileged. If you are not the intended recipient or have
received this email in error, please notify the sender by return email
or by calling 866-434-4976. You should then delete the message and any
attachments or copies. If you are not the intended recipient, you are
prohibited from retaining, distributing, disclosing or using any
information contained herein.
I seem to be having trouble using a conditional OR statement in my code.
Hopefully someone can point out my error.
In one of my controllers I have the following statement:
if @thisVar == (0||1||6)
dosomething
end
I think what you might be trying to do is: if @thisVar =~ /0|1|6/
The | notation for doing an OR is a regular expression thing. You need
to use =~
and enclose your pattern in /…/ to get the regex stuff to work.
If you prefer the list option posted by others, that will work as well.
But I
think the regex pattern match might be a little faster than building a
list and
running a .include. Although it probably doesn’t matter for three
integer values…