From: "Iñaki Baz Castillo" Date: 2008-05-02T23:25:55+09:00 Subject: Re: | versus || Any reason for this issue? 2008/5/2, Robert Klemme : > 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 Thanks to all for those excellent explanations :) -- Iñaki Baz Castillo