↓ Ir para o conteúdo principal

← todas as notas

📎 Webclip

Ruby Metaprogramming - Method Missing

method_missing is a Ruby method for handling calls to methods that do not exist. It gives an object one last chance to deal with the call before Ruby raises an exception. The page shows that it receives the missing method name, any arguments, and an optional block.

Reading notes
#

  • method_missing handles undefined method calls inside an object.
  • It receives three parameters: the method name, *args, and &block.
  • One use is dealing with repetitive methods that behave the same way, such as generating HTML tags dynamically in a DSL.
  • Another use is delegating method calls to another object.
  • A Presenter example stores an object and forwards missing calls with send.
  • A UserPresenter overrides last_name and lets other calls fall through to the wrapped user object.
  • The example shows first_name and age being delegated, while last_name uses the presenter’s own implementation.
  • The page also recreates alias as mimic with a class-level lookup table.
  • When a missing method matches an entry in the table, the call is redirected to the aliased method.
  • If no alias exists, the code raises an ArgumentError.