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

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?

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

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

2008/5/2, Robert K. [email protected]:

=> 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 :slight_smile:

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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs