0
0
Rubyprogramming~10 mins

Method objects with method() 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 get a method object from the string 'hello'.

Ruby
str = 'hello'
method_obj = str.[1](:upcase)
Drag options to blanks, or click blank then click option'
Amethod
Bsend
Ccall
Ddefine_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of method returns the result, not a method object.
Using call directly on the string is invalid.
2fill in blank
medium

Complete the code to call the method object and get the uppercase string.

Ruby
str = 'hello'
method_obj = str.method(:upcase)
result = method_obj.[1]()
Drag options to blanks, or click blank then click option'
Aapply
Bsend
Ccall
Dinvoke
Attempts:
3 left
💡 Hint
Common Mistakes
Using send on a method object is invalid.
Using apply or invoke are not Ruby methods.
3fill in blank
hard

Fix the error in the code to correctly get the length method object from an array.

Ruby
arr = [1, 2, 3]
length_method = arr.[1](:length)
Drag options to blanks, or click blank then click option'
Acall
Bmethod
Csend
Ddefine_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using send calls the method instead of returning the method object.
Using call on the array is invalid.
4fill in blank
hard

Fill both blanks to create a method object for 'reverse' and call it.

Ruby
text = 'world'
reverse_method = text.[1](:reverse)
result = reverse_method.[2]()
Drag options to blanks, or click blank then click option'
Amethod
Bsend
Ccall
Ddefine_method
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of method returns the result immediately.
Using send or define_method to call the method object is invalid.
5fill in blank
hard

Fill all three blanks to get the 'capitalize' method object from a string, call it, and store the result.

Ruby
word = 'ruby'
capitalize_method = word.[1](:capitalize)
result = capitalize_method.[2]()
puts [3]
Drag options to blanks, or click blank then click option'
Amethod
Bcall
Cresult
Dsend
Attempts:
3 left
💡 Hint
Common Mistakes
Using send instead of method to get the method object.
Printing the method object instead of the result.