| versus || Any reason for this issue?

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>

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:

   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

···

On Fri, 2 May 2008, Iñaki Baz Castillo wrote:

--
Rails training from David A. Black and Ruby Power and Light:
   INTRO TO RAILS June 9-12 Berlin
   ADVANCING WITH RAILS June 16-19 Berlin
   INTRO TO RAILS June 24-27 London (Skills Matter)
See http://www.rubypal.com for details and updates!

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

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

  robert

···

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

Thanks to all for those excellent explanations :slight_smile:

···

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>

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

···

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