What is _ _ in Ruby?

I saw this, these are smarterer.com tests. I tested it in irb as follows:

2.2.0 :019 > puts _|_
13
=> nil 

2.2.0 :020 > c = a.to_s.to_i;c+=1;i=13;puts _|_
false
=> nil 

Such a strange behavior. So what is this thing and what should it do?

+4
source share
1 answer

These are three tokens. _and |and _, and it only works in IRB or other environments where defined _.

It:

puts _|_

... identical to this ...

puts _ | _

... which is a simple bitwise OR variable _with itself.

The only reason I can do this is that it will turn nilinto falsein cases where you need a real logical value, and not just a fake value:

irb(main):001:0> nil | nil
=> false

a | a a , |, .

+5

All Articles