Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Ruby, how does puts _|_ work?

Tags:

ruby

irb

puts

I am confused with how puts _|_ works in Ruby. If you type in a variable and then call that statement

3
puts _|_

you get the name of the variable followed by nil

 3
 => nil

However, if you type it again, you get false

puts _|_
=> false

It doesn't seem like one of those Perl-like variables that begin with a dollar sign.

What in the world does this weird symbol mean and how does it work?

like image 886
Richard Hamilton Avatar asked Jun 10 '26 15:06

Richard Hamilton


1 Answers

The underscore in a console (IRB or pry) stands for the result of the previous command. So

3
=> 3
puts _|_
3
=> nil

Here the above puts statement becomes equivalent to

puts 3 <bit-wise or> 3

which puts 3|3 equals puts 3.

Since puts returns nil, when you repeat the puts _|_ it becomes

puts nil|nil

... which is puts false.

like image 167
Mori Avatar answered Jun 12 '26 11:06

Mori



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!