| versus || Any reason for this issue?

  • Thread starter Iñaki Baz Castillo
  • Start date
I

Iñaki Baz Castillo

SGksIHdoeSBpbiB0aGUgc2Vjb25kIGNhc2UgdGhlIG5vbiBkZWNsYXJlZCAibXlfdmFyIiB2YXJp
YWJsZSBpcwpwcm9jZXNzZWQgYnV0IG5vdCBpbiB0aGUgZmlyc3QgY2FzZT8KCmlyYj4gbmlsIHx8
IDIgfHwgbXlfdmFyCj0+IDIKCmlyYj4gbmlsIHwgMiB8IG15X3ZhcgpOYW1lRXJyb3I6IHVuZGVm
aW5lZCBsb2NhbCB2YXJpYWJsZSBvciBtZXRob2QgYG15X3ZhcicKCgpPZiBjb3Vyc2UsIEkgcHJl
ZmVyIHRoZSBmaXJzdCBjYXNlOiAyIGlzIGFscmVhZHkgdHJ1ZSBzbyB0aGVyZSBpcyBubwpyZWFz
b24gdG8gcmVhZCB0aGUgbmV4dCAibXlfdmFyIi4gQWxzbywgaXQncyBub3QgZ29vZCBmb3IgcGVy
Zm9ybWFuY2UsCmlzIGl0PwoKCi0tIApJw7Fha2kgQmF6IENhc3RpbGxvCjxpYmNAYWxpYXgubmV0
Pgo=
 
F

Florian Gilcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1


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
=3D> 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 ||. | needs =20=

to evaluate both Operands while the value of a logical OR (||) is =20
known at the moment that one of the Operands is true.

Regards
Florian Gilcher
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.8 (Darwin)

iEYEARECAAYFAkgbIfUACgkQJA/zY0IIRZakmwCgwtL5xfsC/Z8FRNYN6QaJQn0I
FUQAn39NRwifvYwfJcVurDBzJ1/mzH0i
=3DSLZS
-----END PGP SIGNATURE-----
 
R

Robert Klemme

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
 
R

Robert Klemme

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1




| 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top