0
0
Rubyprogramming~10 mins

Lambda creation and behavior 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 lambda that adds 5 to its input.

Ruby
add_five = ->(x) { x [1] 5 }
Drag options to blanks, or click blank then click option'
A/
B-
C*
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using - instead of + will subtract instead of add.
Using * or / will multiply or divide, which is not the goal.
2fill in blank
medium

Complete the code to call the lambda with argument 10.

Ruby
result = add_five.[1](10)
Drag options to blanks, or click blank then click option'
Aexecute
Bcall
Crun
Dapply
Attempts:
3 left
💡 Hint
Common Mistakes
Using run, execute, or apply will cause errors because these methods don't exist for lambdas.
3fill in blank
hard

Fix the error in the lambda creation syntax.

Ruby
my_lambda = lambda [1] |x| x * 2 }
Drag options to blanks, or click blank then click option'
A[
B(
C{
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or square brackets instead of curly braces causes syntax errors.
4fill in blank
hard

Fill both blanks to create a lambda that checks if a number is even.

Ruby
is_even = ->(n) { n [1] 2 [2] 0 }
Drag options to blanks, or click blank then click option'
A%
B==
C!=
D+
Attempts:
3 left
💡 Hint
Common Mistakes
Using != instead of == will check for odd numbers.
Using + instead of % will add numbers, not check evenness.
5fill in blank
hard

Fill both blanks to create a lambda that returns the uppercase version of a string if its length is greater than 3.

Ruby
transform = ->(s) { s.length [1] 3 ? s.[2] : s }
Drag options to blanks, or click blank then click option'
A<
Bupcase
C>
Ddowncase
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > will invert the condition.
Using downcase instead of upcase changes the string to lowercase.