0
0
Rubyprogramming~15 mins

Proc composition in Ruby - Mini Project: Build & Apply

Choose your learning style9 modes available
Proc composition
📖 Scenario: You are working on a small Ruby program that processes numbers using reusable pieces of code called Procs. Procs are like little machines that take input and give output. You want to combine these machines to create a new machine that does two things in order.
🎯 Goal: Build a Ruby program that creates two simple Procs, then combines them into one Proc that applies both steps in sequence. Finally, run the combined Proc on a number and see the result.
📋 What You'll Learn
Create two Procs named double and increment.
double should multiply its input by 2.
increment should add 1 to its input.
Create a new Proc called double_then_increment that applies double first, then increment.
Call double_then_increment with the number 3 and print the result.
💡 Why This Matters
🌍 Real World
Proc composition is useful when you want to build complex operations from simple reusable steps, like processing data or applying filters.
💼 Career
Understanding Proc composition helps in writing clean, modular Ruby code often used in web development and scripting tasks.
Progress0 / 4 steps
1
Create two Procs
Create a Proc called double that multiplies its input by 2, and another Proc called increment that adds 1 to its input.
Ruby
Need a hint?

Use Proc.new { |x| ... } to create each Proc.

2
Create a composed Proc
Create a new Proc called double_then_increment that takes one argument x, applies double to x, then applies increment to the result.
Ruby
Need a hint?

Inside the new Proc, call double.call(x) first, then pass that result to increment.call(...).

3
Call the composed Proc
Call the Proc double_then_increment with the number 3 and store the result in a variable called result.
Ruby
Need a hint?

Use double_then_increment.call(3) and assign it to result.

4
Print the result
Print the value of the variable result.
Ruby
Need a hint?

Use puts result to display the output.