From: Robert Klemme Date: 2008-05-02T23:20:07+09:00 Subject: Re: | versus || Any reason for this issue? 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. > 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