Instructions
Create a function apply_twice that takes a value and a block,
then applies the block twice to the value.
Use &block to capture a block as a Proc parameter:
ruby
def my_method(&block)
block.call(value) # block is now a Proc
end
Example:
```ruby
apply_twice(2) { |x| x * 3 }
First: 2 * 3 = 6
Second: 6 * 3 = 18
=> 18
Hints:
- Define as def apply_twice(value, &block)
- Call block.call(result) twice
- The & converts block to Proc
Your Code
def apply_twice(value, &block) result = block.call(value) block.call(result) end
RSpec.describe "apply_twice" do
it "applies block twice" do
result = apply_twice(2) { |x| x * 3 }
expect(result).to eq(18)
end
it "works with addition" do
result = apply_twice(5) { |x| x + 10 }
expect(result).to eq(25)
end
it "works with strings" do
result = apply_twice("a") { |s| s + s }
expect(result).to eq("aaaa")
end
end
Results
Click "Run Tests" to see results