0
0
Rubyprogramming~10 mins

Proc composition in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a Proc that doubles a number.

Ruby
double = Proc.new { |x| x [1] 2 }
Drag options to blanks, or click blank then click option'
A-
B+
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using + instead of * will add 2 instead of doubling.
Using - or / will not double the number.
2fill in blank
medium

Complete the code to compose two Procs: one that doubles, then one that adds 3.

Ruby
double = Proc.new { |x| x * 2 }
add_three = Proc.new { |x| x [1] 3 }
composed = Proc.new { |x| add_three.call(double.call(x)) }
Drag options to blanks, or click blank then click option'
A+
B*
C-
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using * will multiply instead of adding.
Using - or / will subtract or divide instead of adding.
3fill in blank
hard

Fix the error in the Proc composition to correctly chain two Procs.

Ruby
square = Proc.new { |x| x * x }
double = Proc.new { |x| x * 2 }
composed = Proc.new { |x| [1].call([2].call(x)) }
Drag options to blanks, or click blank then click option'
Asquare
Bcomposed
Cdouble
Dx
Attempts:
3 left
💡 Hint
Common Mistakes
Calling the same Proc twice causes errors.
Using the wrong Proc name in the call.
4fill in blank
hard

Fill both blanks to create a Proc that adds 1 then multiplies by 5.

Ruby
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)) }
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of addition.
Using division instead of multiplication.
5fill in blank
hard

Fill all three blanks to create a Proc that squares, subtracts 4, then divides by 2.

Ruby
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))) }
Drag options to blanks, or click blank then click option'
A*
B-
C/
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up operators like using + instead of -.
Using multiplication instead of division at the end.