Difference between "and" and "&&"

I thought the only difference between these was precedence, but I’m seeing
another difference.

a = 'test’
b = nil
haveBoth = a and b

haveBoth is now equal to ‘test’

haveBoth = a && b

haveBoth is now equal to nil

Is this the expected behavior?

What I want to do is verify that two strings are not nil.
It looks like I have to use “&&” instead of “and” to do that.

···

WARNING: All e-mail sent to and from this address will be received or
otherwise recorded by the A.G. Edwards corporate e-mail system and is
subject to archival, monitoring or review by, and/or disclosure to,
someone other than the recipient.


Apparently, Volkmann, Mark recently wrote:

I thought the only difference between these was precedence, but I’m
seeing another difference.

a = ‘test’
b = nil
haveBoth = a and b

haveBoth is now equal to ‘test’

haveBoth = a && b

haveBoth is now equal to nil

Is this the expected behavior?

What I want to do is verify that two strings are not nil.
It looks like I have to use “&&” instead of “and” to do that.

This is still the precedence difference: ‘and’ is lower than ‘=’:

Add some parenthesis and you get what you want:

irb(main):001:0> a = ‘test’
“test”
irb(main):002:0> b = nil
nil
irb(main):003:0> haveBoth = a and b
nil
irb(main):004:0> haveBoth
“test”
irb(main):005:0> haveBoth = (a and b)
nil
irb(main):006:0> haveBoth
nil

Hi –

···

On Wed, 20 Nov 2002, Volkmann, Mark wrote:

I thought the only difference between these was precedence, but I’m seeing
another difference.

a = ‘test’
b = nil
haveBoth = a and b

haveBoth is now equal to ‘test’

haveBoth = a && b

haveBoth is now equal to nil

Is this the expected behavior?

Yes. The first one is equivalent to:

(haveBoth = a) and b

The second is:

haveBoth = (a && b)

David


David Alan Black
home: dblack@candle.superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav