# Simple (?) question regarding the "&&" and "and" operators

**URL:** <https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795>\
**Category:** Ruby\
**Created:** [April 17, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795 "2009-04-17T17:12:07Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pmak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@pmak](https://www.ruby-forum.com/u/pmak)\
**Post date:** [April 17, 2009, 5:12pm UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795/1 "2009-04-17T17:12:07Z")

</div>

Hi,

why is

a = Something.new  
b = Something.new

a and b

=\> b

?

I would expect it to return true.  
Same thing goes for the && operator

Regards  
Philip

---

<div class="post-metadata">

**Author:** ![pmak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@pmak](https://www.ruby-forum.com/u/pmak)\
**Post date:** [April 17, 2009, 5:29pm UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795/2 "2009-04-17T17:29:09Z")

</div>

Philip MÃ¼ller wrote:

> a and b
> 
> =\> b
> 
> ?
> 
> I would expect it to return true.

The pseudo-code for and basically looks like this:  
a and b =  
if a: b  
else: a  
This means that if a or b evaluate to a something that counts as false,  
then  
the whole expression will also evaluate to something that counts as  
false.  
And if neither of them do, then the whole expression will evaluate to  
something that counts as true. So for if-conditions etc. the expression  
will  
work as expected.  
The reason that it returns a/b and not true/false is that  
a) there are very few cases where you need the value to be an explicit  
boolean  
b) there are quite a few cases where returning a/b is more meaningful  
and/or  
useful than just returning true/false (though that’s more the case for  
“or”  
than “end”. Example: foo = ARGV[0] or default\_value).

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![pmak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@pmak](https://www.ruby-forum.com/u/pmak)\
**Post date:** [April 17, 2009, 8:51pm UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795/3 "2009-04-17T20:51:03Z")

</div>

On Fri, 17 Apr 2009 17:25:37 +0200, Sebastian H.  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Example: foo = ARGV[0] or default\_value).

Thanks,  
ruby can be different to understand sometimes for someone coming from  
c/c++.

---

<div class="post-metadata">

**Author:** ![pmak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@pmak](https://www.ruby-forum.com/u/pmak)\
**Post date:** [April 17, 2009, 10:28pm UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795/4 "2009-04-17T22:28:17Z")

</div>

Sebastian H. wrote:

> foo = ARGV[0] or default\_value

Be careful. “or” has low precedence!

irb(main):001:0\> default\_value = 12  
=\> 12  
irb(main):002:0\> foo = ARGV[0] or default\_value  
=\> 12  
irb(main):003:0\> foo  
=\> nil  
irb(main):004:0\> foo = ARGV[0] || default\_value  
=\> 12  
irb(main):005:0\> foo  
=\> 12

---

<div class="post-metadata">

**Author:** ![pmak](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/5fc32e/32.png) [@pmak](https://www.ruby-forum.com/u/pmak)\
**Post date:** [April 18, 2009, 9:35am UTC](https://www.ruby-forum.com/t/simple-question-regarding-the-and-and-operators/165795/5 "2009-04-18T09:35:40Z")

</div>

On 17.04.2009 20:47, Philip Müller wrote:

> On Fri, 17 Apr 2009 17:25:37 +0200, Sebastian H.  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > Example: foo = ARGV[0] or default\_value).
> 
> Thanks,  
> ruby can be different to understand sometimes for someone coming from  
> c/c++.

Yes, maybe. But if you get the hang of it you will see how useful this  
is. Actually what the expression returns is a value which is “true”  
equivalent, i.e. you can do things like

if a && b  
puts “both set”  
end

But also, and this is where the fact is handy that one of the values is  
returned (the first non false and non nil in this case):

x = a || b

In C/C++ you might have to do something like

x = a == NULL ? b : a;

or maybe just

x = a ? a : b;

(My C has become a bit rusty.)

Kind regards

robert
