dsounded
dsounded

Reputation: 723

What is it _|_ in Ruby language?

I've seen this thing it smarterer.com tests. I've tested it in irb like this:

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

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

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

Upvotes: 4

Views: 109

Answers (1)

user229044
user229044

Reputation: 239312

It's three tokens. _, and |, and _, and it only works in IRB or other environments where _ is defined.

This:

puts _|_

...is identical to this...

puts _ | _

...which is a simple bitwise OR of the variable _ with itself.

The only reason I can think of to do this is that it will turn nil into false in cases where you need a real boolean, not just a falsy value:

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

Otherwise, a | a is always going to be a for types that support |, with the notable exception of some built-in types.

Upvotes: 5

Related Questions