Forum: Ruby on Rails Rails/Ruby OR

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.
0b8601544389bf0603f63bfe5afae865?d=identicon&s=25 nmealey (Guest)
on 2005-11-21 15:51
(Received via mailing list)
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 Mealey

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.
A43430351a6d4fb6d61d000bd56ed19a?d=identicon&s=25 francois (Guest)
on 2005-11-21 15:59
(Received via mailing list)
one way would be:

if [0,1,6].include?(@thisVar)
dosomething
end
24d2f8804e6bb4b7ea6bd11e0a586470?d=identicon&s=25 jeremy (Guest)
on 2005-11-21 15:59
(Received via mailing list)
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Nov 21, 2005, at 6:44 AM, Nathan Mealey 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-----
7c4087d053eb02d099a17d91ba5e33b5?d=identicon&s=25 brianvh (Guest)
on 2005-11-21 16:15
(Received via mailing list)
Nathan Mealey 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
27c170f482104299af279902be0a9c26?d=identicon&s=25 trevor (Guest)
on 2005-11-21 17:08
(Received via mailing list)
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 Mealey 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
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>
--
Trevor Squires
http://somethinglearned.com
Acd864a79c581fd5f7203ec993896c10?d=identicon&s=25 Deepak Surti <dmsurti@gmail.com> (Guest)
on 2005-11-21 20:19
@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.
Fee23d1fc58edee59e05d7a52dcf172e?d=identicon&s=25 blargity (Guest)
on 2005-11-21 21:55
(Received via mailing list)
On Monday 21 November 2005 13:19, Deepak Surti <dmsurti@gmail.com>
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. :-)
992fe8c19bbbc27f2b562a9f96efc03d?d=identicon&s=25 jamis (Guest)
on 2005-11-21 22:51
(Received via mailing list)
On Nov 21, 2005, at 1:51 PM, Kevin Brown wrote:

> On Monday 21 November 2005 13:19, Deepak Surti <dmsurti@gmail.com>
> 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. :-)

A minor correction to your minor correction of a minor correction. ;)

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

- Jamis
E555e7c34196967444a47a96395a23ab?d=identicon&s=25 skaes (Guest)
on 2005-11-22 02:20
(Received via mailing list)
Nathan Mealey wrote:

>
>
>
Others have explained why your code won't work.

You could try

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

-- stefan
This topic is locked and can not be replied to.