[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

If || statements

John Maclean

2/9/2008 12:29:00 AM

Hey chaps,

Is is at all possible to have a statement such as

p "some string " if ( stuff || more_stuff || other_stuff)



--

Regards,

John Maclean

6 Answers

Tim Hunter

2/9/2008 12:36:00 AM

0

John Maclean wrote:
> Hey chaps,
>
> Is is at all possible to have a statement such as
>
> p "some string " if ( stuff || more_stuff || other_stuff)
>
>
>

You don't have to ask. Try it yourself. Start up irb and enter a statement:

$ irb
irb(main):001:0> x = 2
=> 2
irb(main):002:0> y = 3
=> 3
irb(main):003:0> z = "a"
=> "a"
irb(main):004:0> p "some string" if (x || y || z)
"some string"
=> nil
irb(main):005:0>


--
RMagick: http://rmagick.ruby...
RMagick 2: http://rmagick.ruby...rmagick2.html

Felix Windt

2/9/2008 12:39:00 AM

0

On Sat, 2008-02-09 at 09:28 +0900, John Maclean wrote:
> Hey chaps,
>
> Is is at all possible to have a statement such as
>
> p "some string " if ( stuff || more_stuff || other_stuff)
>
>
>

I'm not sure what I'm missing here:

$ irb
irb(main):001:0> p "hello" if (false || false || true)
"hello"
=> nil
irb(main):002:0> p "hello" if (false || false)
=> nil


HTH,

Felix


Robert Klemme

2/9/2008 11:10:00 AM

0

On 09.02.2008 01:36, Tim Hunter wrote:
> John Maclean wrote:
>> Hey chaps,
>>
>> Is is at all possible to have a statement such as
>>
>> p "some string " if ( stuff || more_stuff || other_stuff)
>>
>>
>>
>
> You don't have to ask. Try it yourself. Start up irb and enter a statement:
>
> $ irb
> irb(main):001:0> x = 2
> => 2
> irb(main):002:0> y = 3
> => 3
> irb(main):003:0> z = "a"
> => "a"
> irb(main):004:0> p "some string" if (x || y || z)
> "some string"
> => nil
> irb(main):005:0>

And you can spare the brackets

irb(main):001:0> "yes" if false || false || true
=> "yes"

Cheers

robert

Dominik Honnef

2/13/2008 1:36:00 PM

0

On [Sat, 09.02.2008 09:39], fw wrote:
> On Sat, 2008-02-09 at 09:28 +0900, John Maclean wrote:
> > Hey chaps,
> >
> > Is is at all possible to have a statement such as
> >
> > p "some string " if ( stuff || more_stuff || other_stuff)
> >
> >
> >
>
> I'm not sure what I'm missing here:
>
> $ irb
> irb(main):001:0> p "hello" if (false || false || true)
> "hello"
> => nil
> irb(main):002:0> p "hello" if (false || false)
> => nil
>
>
> HTH,
>
> Felix

Uhm, it's working just the way everybody would expect it to work?
false or false or true evaluates to true (if i have the choice i of course take the best one)
false or false is just false, as there's no way to pick true.
--
Dominik Honnef

Dominik Honnef

2/13/2008 6:18:00 PM

0

Oh sorry, forgot to have a look on the senders, thought you were the one who started this thread.
--
Dominik Honnef

Rha

2/14/2008 10:07:00 PM

0

|| operator return true if at least one of the operands is true, so:

false || false || true returns true, and hello is printed.

false || false returns false and thus, nothing is printed (nil).

Note: || operator stops evaluating when the first true yielding
expression is found.


On Fri, 8 Feb 2008 19:39:06 -0500, fw <fwmailinglists@gmail.com>
wrote:

>On Sat, 2008-02-09 at 09:28 +0900, John Maclean wrote:
>> Hey chaps,
>>
>> Is is at all possible to have a statement such as
>>
>> p "some string " if ( stuff || more_stuff || other_stuff)
>>
>>
>>
>
>I'm not sure what I'm missing here:
>
>$ irb
>irb(main):001:0> p "hello" if (false || false || true)
>"hello"
>=> nil
>irb(main):002:0> p "hello" if (false || false)
>=> nil
>
>
>HTH,
>
>Felix
>