Rails/Ruby OR

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.

one way would be:

if [0,1,6].include?(@thisVar)
dosomething
end

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 21, 2005, at 6:44 AM, Nathan M. wrote:

I seem to be having trouble using a conditional OR statement in my
code. Hopefully someone can point out my error.

[0,1,6].include?(@thisVar)

jeremy
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (Darwin)

iD8DBQFDgeBYAQHALep9HFYRAuiyAJ4gqEni+ZWmNPDO0VQT3GH6XLaMdwCeL0mP
Q/4yHladEt0gaj9SYV5dd28=
=aiIo
-----END PGP SIGNATURE-----

Nathan M. wrote:

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…

-Brian

@thisVar = (0 || 1 || 6)

0 || 1 || 6 will evaluate to true…

so your code fails.

you are comparing @thisVar to true not to 0,1,6.

Hence either comparing to each value or case or multiple ifs is the way
out.

On Monday 21 November 2005 13:19, Deepak S. [email protected]
wrote:

@thisVar = (0 || 1 || 6)

Minor correction to your minor correction.

@thisVar == (0 || 1 || 6) compares to true. The code above will assign
true
to @thisVar. :slight_smile:

Hi Nathan,

despite some of the very clever responses you’ve gotten to your query I
suspect you’re missing a rather more fundamental answer to your
question:

if @thisVar == 0 || @thisVar == 1 || @thisVar == 6
doSomething
end

I suspect this is the conditional OR you are looking for otherwise
you’d have never suggested typing “doSomething” three times.

Regards,
Trevor

On 21-Nov-05, at 6:44 AM, Nathan M. wrote:

if @thisVar == (0||1||6)

dosomething
I have had this same problem in .rhtml code, when I use something like:

FAX 502.721.7940
information contained herein.


Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails


Trevor S.
http://somethinglearned.com

Nathan M. wrote:

Others have explained why your code won’t work.

You could try

case @thisVar; when 0,1,6 then
dosomething
end

– stefan

On Nov 21, 2005, at 1:51 PM, Kevin B. wrote:

On Monday 21 November 2005 13:19, Deepak S. [email protected]
wrote:

@thisVar = (0 || 1 || 6)

Minor correction to your minor correction.

@thisVar == (0 || 1 || 6) compares to true. The code above will
assign true
to @thisVar. :slight_smile:

A minor correction to your minor correction of a minor correction. :wink:

@thisVar = (0 || 1 || 6) assigns 0 to @thisVar.

  • Jamis