Complete the code to create a Proc that doubles a number.
double = Proc.new { |x| x [1] 2 }The * operator multiplies the number by 2, doubling it.
Complete the code to compose two Procs: one that doubles, then one that adds 3.
double = Proc.new { |x| x * 2 }
add_three = Proc.new { |x| x [1] 3 }
composed = Proc.new { |x| add_three.call(double.call(x)) }The + operator adds 3 to the doubled number.
Fix the error in the Proc composition to correctly chain two Procs.
square = Proc.new { |x| x * x }
double = Proc.new { |x| x * 2 }
composed = Proc.new { |x| [1].call([2].call(x)) }To double then square, call square.call(double.call(x)). The outer blank should be square (A) and the inner blank double (C).
Fill both blanks to create a Proc that adds 1 then multiplies by 5.
add_one = Proc.new { |x| x [1] 1 }
multiply_five = Proc.new { |x| x [2] 5 }
composed = Proc.new { |x| multiply_five.call(add_one.call(x)) }The first Proc adds 1 using +, the second multiplies by 5 using *.
Fill all three blanks to create a Proc that squares, subtracts 4, then divides by 2.
square = Proc.new { |x| x [1] x }
subtract_four = Proc.new { |x| x [2] 4 }
divide_two = Proc.new { |x| x [3] 2 }
composed = Proc.new { |x| divide_two.call(subtract_four.call(square.call(x))) }The first Proc squares the number using *, the second subtracts 4 using -, and the third divides by 2 using /.