📖 Scenario: Imagine you are processing a list of numbers to prepare a report. You want to square each number, then convert it to a string with a prefix, and finally collect all results.
🎯 Goal: Build a Ruby program that uses the pipeline operator |> to chain methods for transforming a list of numbers step-by-step.
📋 What You'll Learn
Create an array called numbers with the values [1, 2, 3, 4, 5].
Create a variable called prefix and set it to the string "Number: ".
Use the pipeline operator |> to square each number, then convert each to a string with the prefix.
Print the final transformed array.
💡 Why This Matters
🌍 Real World
The pipeline operator helps write clear and readable code when processing data step-by-step, like transforming lists of values in reports or data analysis.
💼 Career
Understanding the pipeline operator is useful for Ruby developers to write clean, functional-style code that is easy to maintain and extend.
Progress0 / 4 steps
1
Create the initial array of numbers
Create an array called numbers with the exact values [1, 2, 3, 4, 5].
Ruby
Hint
Use square brackets [] to create an array with the numbers 1 through 5.
2
Add a prefix string variable
Create a variable called prefix and set it to the string "Number: ".
Ruby
Hint
Use = to assign the string "Number: " to the variable prefix.
3
Use the pipeline operator to transform numbers
Use the pipeline operator |> to create a new array called result that squares each number in numbers, then converts each squared number to a string prefixed by prefix. Use map twice in the chain.
Ruby
Hint
Use |> to chain two map calls: first to square, second to add prefix and convert to string.
4
Print the final result
Write a puts statement to print the result array.
Ruby
Hint
Use puts result to print each element of the array on its own line.
Practice
(1/5)
1.
What does the Ruby pipeline operator |> do?
easy
A. It creates a new array from two arrays.
B. It combines two strings into one.
C. It passes the value on the left as the first argument to the method on the right.
D. It comments out the code on the right.
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 C
Quick Check:
Pipeline operator = Pass left value as first argument [OK]
Hint: Remember: left value flows into right method as first argument [OK]
Common Mistakes:
Thinking it concatenates strings
Confusing it with array merging
Assuming it comments code
2.
Which of the following is the correct syntax to use the pipeline operator in Ruby?
value ___ method_name
easy
A. |>
B. |
C. ->
D. |>
Solution
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 A
Quick Check:
Correct pipeline syntax = |> [OK]
Hint: Look for the exact two-character operator: pipe then greater than [OK]
Common Mistakes:
Using -> instead of |> operator
Adding extra characters like
Missing the greater than symbol
3.
What is the output of the following Ruby code?
def double(x)
x * 2
end
result = 5 |> double
puts result
medium
A. 25
B. 5double
C. SyntaxError
D. 10
Solution
Step 1: Understand the pipeline operation
The value 5 is passed as the first argument to the method double, so it calls double(5).
Step 2: Calculate the method result
double(5) returns 5 * 2 = 10, so result is 10, which is printed.
Final Answer:
10 -> Option D
Quick Check:
5 |> double = 10 [OK]
Hint: Pipeline passes 5 to double, so output doubles 5 [OK]
Common Mistakes:
Thinking it concatenates '5' and 'double'
Expecting a syntax error
Multiplying 5 by itself instead of 2
4.
Identify the error in this Ruby code using the pipeline operator:
A. The pipeline operator is missing the right side method.
B. The pipeline operator is split incorrectly across lines.
C. The pipeline operator should be -> instead of |>.
D. The method greet should not use string interpolation.
Solution
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 method greet is 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 B
Quick Check:
Pipeline operator must not be broken by newline [OK]
Hint: Keep pipeline operator and method call on same line [OK]
Common Mistakes:
Splitting pipeline operator and method on different lines
Confusing pipeline with lambda syntax
Blaming string interpolation for error
5.
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 result
hard
A. 16
B. 10
C. 7
D. 9
Solution
Step 1: Apply the first pipeline operation
The value 3 is passed to add_one, so add_one(3) returns 4.
Step 2: Apply the second pipeline operation
The result 4 is then passed to square, so square(4) returns 16.
Final Answer:
16 -> Option A
Quick Check:
3 |> add_one |> square = 16 [OK]
Hint: Pipeline chains: left to right, each result feeds next method [OK]
Common Mistakes:
Calculating square before add_one
Adding 1 after squaring
Confusing order of operations
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.