Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ruby pipe operator

Tags:

I'm new to ruby, and I saw this code snippet

1|2 

and it returns 3

What does the | operator actually do? I couldn't seem to find any documentation on it. Also, in this context is it referred to as the "pipe" operator? or is it called something else?

like image 616
Jeff Storey Avatar asked Jun 22 '12 22:06

Jeff Storey


People also ask

What is the pipe operator in Ruby?

The pipeline operator is used to provide the output of the left side to the input of the right side. It explicitly requires that this be the sole argument to the function, of which can be circumvented with currying techniques in both languages to partially apply functions.

What is the function of pipe operator?

What does the pipe do? The pipe operator, written as %>% , has been a longstanding feature of the magrittr package for R. It takes the output of one function and passes it into another function as an argument. This allows us to link a sequence of analysis steps.

What is double pipe in Ruby?

So, what exactly does ||= do? My own definition is that “double-pipe equals” is an operator that assigns a value, much like = or our classic assignment operator, but will only complete the assignment if the left side of our operation returns false or nil .

Is there a pipe operator in Python?

Pipe is a Python library that enables you to use pipes in Python. A pipe ( | ) passes the results of one method to another method. I like Pipe because it makes my code look cleaner when applying multiple methods to a Python iterable. Since Pipe only provides a few methods, it is also very easy to learn Pipe.


1 Answers

This is a bitwise operator and they work directly with the binary representation of the value.

| mean OR. Let me show you how it works.

1|2 = 3 what happens under the hoods is:

1 = 0001 2 = 0010 -------- 3 = 0011 <- result 

another example:

10|2 = 10 now in binary:

10 = 1010 2  = 0010 -------- 10 = 1010 <- result 
like image 72
roshiro Avatar answered Sep 30 '22 14:09

roshiro