# | versus || Any reason for this issue?

**URL:** <https://rubytalk.org/t/versus-any-reason-for-this-issue/46250>\
**Category:** ruby-talk\
**Created:** [2 May 2008 14:01 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250 "2008-05-02T14:01:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [2 May 2008 14:01 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/1 "2008-05-02T14:01:32Z")

</div>

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

> nil || 2 || my\_var

=\> 2

> 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?

> **···**
>
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![David\_A\_Black1](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@David\_A\_Black1](https://rubytalk.org/u/David_A_Black1)\
**Post date:** [2 May 2008 14:13 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/2 "2008-05-02T14:13:52Z")

</div>

Hi --

> Hi, why in the second case the non declared "my\_var" variable is  
> processed but not in the first case?
> 
> \> nil || 2 || my\_var  
> =\> 2
> 
> \> 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?

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:

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

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

&nbsp;&nbsp;&nbsp;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

> **···**
>
> On Fri, 2 May 2008, Iñaki Baz Castillo wrote:
> 
> --  
> Rails training from David A. Black and Ruby Power and Light:  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 9-12 Berlin  
> &nbsp;&nbsp;&nbsp;ADVANCING WITH RAILS June 16-19 Berlin  
> &nbsp;&nbsp;&nbsp;INTRO TO RAILS June 24-27 London (Skills Matter)  
> See [http://www.rubypal.com](http://www.rubypal.com) for details and updates!

---

<div class="post-metadata">

**Author:** ![Florian\_Gilcher](https://avatars.discourse-cdn.com/v4/letter/f/57b2e6/32.png) [@Florian\_Gilcher](https://rubytalk.org/u/Florian_Gilcher)\
**Post date:** [2 May 2008 14:15 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/3 "2008-05-02T14:15:30Z")

</div>

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 Gilcher

> **···**
>
> On May 2, 2008, at 4:01 PM, Iñaki Baz Castillo wrote:
> 
> > Hi, why in the second case the non declared "my\_var" variable is  
> > processed but not in the first case?
> > 
> > \> nil || 2 || my\_var  
> > =\> 2
> > 
> > \> 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 ||. | needs

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [2 May 2008 14:20 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/4 "2008-05-02T14:20:07Z")

</div>

> 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.

> \> nil || 2 || my\_var  
> =\> 2
> 
> \> 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

&nbsp;&nbsp;robert

> **···**
>
> On 02.05.2008 16:01, Iñaki Baz Castillo wrote:

---

<div class="post-metadata">

**Author:** ![Inaki\_Baz\_Castillo](https://avatars.discourse-cdn.com/v4/letter/i/f14d63/32.png) [@Inaki\_Baz\_Castillo](https://rubytalk.org/u/Inaki_Baz_Castillo)\
**Post date:** [2 May 2008 14:25 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/5 "2008-05-02T14:25:55Z")

</div>

Thanks to all for those excellent explanations 🙂

> **···**
>
> 2008/5/2, Robert Klemme \<shortcutter@googlemail.com\>:
> 
> > On 02.05.2008 16:01, Iñaki Baz Castillo 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.
> > 
> > \> \> nil || 2 || my\_var  
> > \> =\> 2  
> > \>  
> > \> \> 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
> 
> --  
> Iñaki Baz Castillo  
> \<ibc@aliax.net\>

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [2 May 2008 14:35 UTC](https://rubytalk.org/t/versus-any-reason-for-this-issue/46250/6 "2008-05-02T14:35:04Z")

</div>

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

&nbsp;&nbsp;robert

> **···**
>
> On 02.05.2008 16:15, Florian Gilcher wrote:
> 
> > -----BEGIN PGP SIGNED MESSAGE-----  
> > Hash: SHA1
> > 
> > On May 2, 2008, at 4:01 PM, Iñaki Baz Castillo wrote:
> > 
> > > Hi, why in the second case the non declared "my\_var" variable is  
> > > processed but not in the first case?
> > > 
> > > \> nil || 2 || my\_var  
> > > =\> 2
> > > 
> > > \> 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 ||.
