# | versus || Any reason for this issue?

**URL:** <https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485>\
**Category:** Ruby\
**Created:** [May 2, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485 "2008-05-02T16:02:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:02pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/1 "2008-05-02T16:02:18Z")

</div>

Hi, why in the second case the non declared “my\_var” variable is  
processed but not in the first case?

irb\> nil || 2 || my\_var  
=\> 2

irb\> nil | 2 | my\_var  
NameError: undefined local variable or method `my\_var’

Of course, I prefer the first case: 2 is already true so there is no  
reason to read the next “my\_var”. Also, it’s not good for performance,  
is it?

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:14pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/2 "2008-05-02T16:14:42Z")

</div>

Hi –

On Fri, 2 May 2008, IÃ±aki Baz C. wrote:

> Of course, I prefer the first case: 2 is already true so there is no  
> reason to read the next “my\_var”. Also, it’s not good for performance,  
> is it?

The single bar and the double bar are not related. | is a method that  
any class or object can define. For example, Fixnums use it as a  
bitwise operator:

4 | 1 # 5 (i.e., 100 | 001 == 101)

The single bar doesn’t short circuit, whereas the || operator does.

nil || 2 || 1/0 # 2 (it never reaches 1/0 so doesn’t blow up)

If you do nil || my\_var, my\_var will be reached and will give you an  
error if it isn’t defined.

David

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:15pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/3 "2008-05-02T16:15:53Z")

</div>

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

On May 2, 2008, at 4:01 PM, Iñaki Baz C. wrote:

> Of course, I prefer the first case: 2 is already true so there is no  
> reason to read the next “my\_var”. Also, it’s not good for performance,  
> is it?

| is the bitwise OR and something entirely different from ||. | needs  
to evaluate both Operands while the value of a logical OR (||) is  
known at the moment that one of the Operands is true.

Regards  
Florian G.  
-----BEGIN PGP SIGNATURE-----  
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgbIfUACgkQJA/zY0IIRZakmwCgwtL5xfsC/Z8FRNYN6QaJQn0I  
FUQAn39NRwifvYwfJcVurDBzJ1/mzH0i  
=SLZS  
-----END PGP SIGNATURE-----

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:26pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/4 "2008-05-02T16:26:32Z")

</div>

2008/5/2, Robert K. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > =\> 2  
> > “|” on the other hand is an operator that can be overloaded and it does not  
> > FalseClass  
> > TrueClass  
> > NilClass

Thanks to all for those excellent explanations 🙂

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:35pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/5 "2008-05-02T16:35:26Z")

</div>

On 02.05.2008 16:15, Florian G. wrote:

> > =\> 2
> > 
> > irb\> nil | 2 | my\_var  
> > NameError: undefined local variable or method `my\_var’
> > 
> > Of course, I prefer the first case: 2 is already true so there is no  
> > reason to read the next “my\_var”. Also, it’s not good for performance,  
> > is it?
> 
> | is the bitwise OR and something entirely different from ||.

This is only mostly correct. Semantics of | depend on the class that  
implements it.

irb(main):001:0\> %w{a b c} | %w{c d e}  
=\> [“a”, “b”, “c”, “d”, “e”]  
irb(main):002:0\> %w{a b c} & %w{c d e}  
=\> [“c”]

Cheers

robert

---

<div class="post-metadata">

**Author:** ![ISSSSaki\_Baz\_C](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/51bf81/32.png) [@ISSSSaki\_Baz\_C](https://www.ruby-forum.com/u/ISSSSaki_Baz_C)\
**Post date:** [May 2, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/versus-any-reason-for-this-issue/136485/6 "2008-05-02T16:21:11Z")

</div>

On 02.05.2008 16:01, Iñaki Baz C. wrote:

> Hi, why in the second case the non declared “my\_var” variable is  
> processed but not in the first case?

Because || does short circuit while | does not.

> irb\> nil || 2 || my\_var  
> =\> 2
> 
> irb\> nil | 2 | my\_var  
> NameError: undefined local variable or method `my\_var’
> 
> Of course, I prefer the first case: 2 is already true so there is no  
> reason to read the next “my\_var”. Also, it’s not good for performance,  
> is it?

“||” is the short circuit boolean “or” operator. You cannot override  
it. “|” on the other hand is an operator that can be overloaded and it  
does not short circuit. It would not make any sense to make “|” short  
circuit because it can contain arbitrary functionality.

Here are some classes that implement “|”:

$ ruby -e ‘ObjectSpace.each\_object(Module) {|cl| p cl if  
cl.instance\_methods.include? “|”}’  
Array  
Bignum  
Fixnum  
FalseClass  
TrueClass  
NilClass

Kind regards

robert
