0
0
Rubyprogramming~10 mins

Method_missing for catch-all 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 define a method that catches calls to undefined methods.

Ruby
def [1](method_name)
  puts "You called #{method_name}!"
end
Drag options to blanks, or click blank then click option'
Amethod_missing
Bundefined_method
Cmissing_method
Dcatch_all
Attempts:
3 left
💡 Hint
Common Mistakes
Using a method name other than 'method_missing' will not catch undefined methods.
2fill in blank
medium

Complete the code to print the name of the missing method and its arguments.

Ruby
def method_missing(method_name, *[1])
  puts "Called #{method_name} with #{args.inspect}"
end
Drag options to blanks, or click blank then click option'
Aargs
Bparams
Carguments
Dvalues
Attempts:
3 left
💡 Hint
Common Mistakes
Using a variable name other than 'args' without updating the code inside the method.
3fill in blank
hard

Fix the error in the method_missing definition to correctly handle missing methods with arguments.

Ruby
def method_missing(method_name, [1])
  puts "Missing method: #{method_name}"
end
Drag options to blanks, or click blank then click option'
Aparams
Bargs
C*args
D&block
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the splat operator causes argument errors when multiple arguments are passed.
4fill in blank
hard

Fill both blanks to define method_missing that prints method name and block presence.

Ruby
def method_missing(method_name, *args, &[1])
  if [2]
    puts "Block given to #{method_name}"
  else
    puts "No block given to #{method_name}"
  end
end
Drag options to blanks, or click blank then click option'
Ablock
Bargs
Cblock_given?
Dmethod_name
Attempts:
3 left
💡 Hint
Common Mistakes
Not capturing the block argument or not checking for block presence correctly.
5fill in blank
hard

Fill all three blanks to define method_missing that logs method name, arguments, and returns a default message.

Ruby
def method_missing([1], *[2], &block)
  puts "Called #{method_name} with #{args.inspect}"
  "Default response for #[3]"
end
Drag options to blanks, or click blank then click option'
Amethod_name
Bargs
Dparams
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent variable names or missing the splat operator for arguments.