The pipeline operator helps you write code that flows step-by-step, making it easier to read and understand.
Pipeline operator concept in Ruby
value |> method1 |> method2 |> method3
The |> operator passes the result of the left side as the first argument to the method on the right.
This operator is available in Ruby 2.6 and later as an experimental feature, and officially introduced in Ruby 3.1.
5 |> ->(x) { x + 1 } |> ->(x) { x * 2 }
"hello" |> :upcase.to_proc |> :reverse.to_proc[1, 2, 3] |> ->(arr) { arr.map { |x| x * 3 } } |> ->(arr) { arr.sum }
This program subtracts 2 from 10, multiplies the result by 5, then converts it to a string with " units" added.
result = 10 |> ->(x) { x - 2 } |> ->(x) { x * 5 } |> ->(x) { x.to_s + " units" } puts result
The pipeline operator makes code easier to read by avoiding nested calls.
You can use lambdas or method references on the right side of |>.
Remember this operator is new in Ruby 3.1, so older Ruby versions won't support it without enabling experimental features.
The pipeline operator |> passes the left value as the first argument to the right method.
It helps write clear, step-by-step code without nested calls.
Use it to improve readability and flow in your Ruby programs.
Practice
What does the Ruby pipeline operator |> do?
Solution
Step 1: Understand the operator's purpose
The pipeline operator|>takes the value on its left side and sends it as the first argument to the method on its right side.Step 2: Compare with other options
Options B, C, and D describe unrelated operations like string concatenation, array merging, or commenting, which are not what|>does.Final Answer:
It passes the value on the left as the first argument to the method on the right. -> Option CQuick Check:
Pipeline operator = Pass left value as first argument [OK]
- Thinking it concatenates strings
- Confusing it with array merging
- Assuming it comments code
Which of the following is the correct syntax to use the pipeline operator in Ruby?
value ___ method_nameSolution
Step 1: Identify the correct operator symbol
The Ruby pipeline operator is written as|>without any extra characters or spaces.Step 2: Check each option
-> uses an arrow->which is for lambdas, not pipelines. | is just a pipe character, not the pipeline operator. |> has an extra closing tag which is invalid syntax.Final Answer:
|> -> Option AQuick Check:
Correct pipeline syntax = |> [OK]
- Using -> instead of |> operator
- Adding extra characters like
- Missing the greater than symbol
What is the output of the following Ruby code?
def double(x)
x * 2
end
result = 5 |> double
puts resultSolution
Step 1: Understand the pipeline operation
The value 5 is passed as the first argument to the methoddouble, so it callsdouble(5).Step 2: Calculate the method result
double(5)returns 5 * 2 = 10, soresultis 10, which is printed.Final Answer:
10 -> Option DQuick Check:
5 |> double = 10 [OK]
- Thinking it concatenates '5' and 'double'
- Expecting a syntax error
- Multiplying 5 by itself instead of 2
Identify the error in this Ruby code using the pipeline operator:
def greet(name)
"Hello, #{name}!"
end
message = "Alice" |>
greet
puts messageSolution
Step 1: Check pipeline operator usage across lines
The pipeline operator|>must be on the same line as the left value or properly continued. Here, it is split with a newline after|>, which causes a syntax error.Step 2: Verify other options
The pipeline operator is missing the right side method. is wrong because the right side methodgreetis present. The pipeline operator should be->instead of|>. confuses the operator with lambda syntax. The method greet should not use string interpolation. is unrelated to the pipeline error.Final Answer:
The pipeline operator is split incorrectly across lines. -> Option BQuick Check:
Pipeline operator must not be broken by newline [OK]
- Splitting pipeline operator and method on different lines
- Confusing pipeline with lambda syntax
- Blaming string interpolation for error
Given the following methods, what is the output of this Ruby code using the pipeline operator?
def add_one(x)
x + 1
end
def square(x)
x * x
end
result = 3 |> add_one |> square
puts resultSolution
Step 1: Apply the first pipeline operation
The value 3 is passed toadd_one, soadd_one(3)returns 4.Step 2: Apply the second pipeline operation
The result 4 is then passed tosquare, sosquare(4)returns 16.Final Answer:
16 -> Option AQuick Check:
3 |> add_one |> square = 16 [OK]
- Calculating square before add_one
- Adding 1 after squaring
- Confusing order of operations
Ready to prove it?
Pass the test and this pattern moves unknown
