0
0
Rubyprogramming~10 mins

Proc creation and call 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 returns 'Hello'.

Ruby
greet = Proc.new { [1] }
Drag options to blanks, or click blank then click option'
Areturn "Hello"
Bputs "Hello"
C"Hello"
Dprint "Hello"
Attempts:
3 left
💡 Hint
Common Mistakes
Using puts or print inside the Proc returns nil instead of the string.
Using return inside a Proc block causes an error.
2fill in blank
medium

Complete the code to call the Proc and print its result.

Ruby
greet = Proc.new { "Hello" }
puts [1]
Drag options to blanks, or click blank then click option'
Agreet.call()
Bgreet()
Cgreet.call
Dgreet
Attempts:
3 left
💡 Hint
Common Mistakes
Using just the Proc variable name prints the Proc object, not its result.
Using greet() without call is invalid syntax for Proc.
3fill in blank
hard

Fix the error in the code to correctly create and call a Proc that adds 5 to a number.

Ruby
add_five = Proc.new { |num| num [1] 5 }
result = add_five.call(10)
puts result
Drag options to blanks, or click blank then click option'
A+
B-
C*
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction or multiplication changes the intended result.
Using division causes unexpected results or errors.
4fill in blank
hard

Fill both blanks to create a Proc that checks if a number is even and call it with 8.

Ruby
is_even = Proc.new { |n| n [1] 2 == 0 }
puts is_even.[2](8)
Drag options to blanks, or click blank then click option'
A%
Bcall
C==
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using == inside the block incorrectly.
Trying to call the Proc without call method.
5fill in blank
hard

Fill all three blanks to create a Proc that multiplies a number by 3, call it with 7, and print the result.

Ruby
triple = Proc.new { |x| x [1] 3 }
result = triple.[2]([3])
puts result
Drag options to blanks, or click blank then click option'
A*
Bcall
C7
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Forgetting to call the Proc with an argument.
Passing the argument incorrectly.