Instructions
Ruby 3.4 introduced it as a shorthand for single-parameter blocks.
Instead of:
ruby
[1, 2, 3].map { |n| n * 2 }
You can write:
ruby
[1, 2, 3].map { it * 2 }
Create a function double_all that doubles each number using it.
Hints:
- Use .map { it * 2 }
- 'it' refers to the block parameter automatically
- This only works in Ruby 3.4+
Your Code
def double_all(numbers)
numbers.map { it * 2 }
end
RSpec.describe "double_all with 'it'" do
it "doubles each number" do
expect(double_all([1, 2, 3])).to eq([2, 4, 6])
end
it "handles empty array" do
expect(double_all([])).to eq([])
end
it "handles negative numbers" do
expect(double_all([-1, -2])).to eq([-2, -4])
end
end
Results
Click "Run Tests" to see results
1 / 3
Next: Chaining with 'it'