0Pricing
Ruby Academy · Lesson

Dynamic Method Definition

Learn how to define and call methods dynamically using define_method and method_missing.

1

Dynamic Method Definition

In Ruby, methods can be created dynamically at runtime.

This allows for more flexible and reusable code.

Dynamic Method Definition — illustration 1

2

Using define_method

The define_method method allows defining methods dynamically.

class Person
  define_method(:greet) { puts 'Hello!' }
end

p = Person.new
p.greet

All lessons in this course

  1. What is Metaprogramming?
  2. Dynamic Method Definition
  3. Open Classes and Monkey Patching
  4. Using send and eval
← Back to Ruby Academy