Ruby Academy · Lesson

Dynamic Method Definition

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

Lesson 2 of 410 steps

Dynamic Method Definition is a free Ruby Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.

Creating Methods Dynamically

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

Using define_method

The define_method method allows defining methods dynamically.

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

p = Person.new
p.greet

Using define_method with Parameters

You can pass arguments to dynamically created methods.

class Person
  define_method(:greet) { |name| puts "Hello, #{name}!" }
end

p = Person.new
p.greet('Alice')

Using method_missing

The method_missing method intercepts calls to undefined methods.

class Person
  def method_missing(name, *args)
    puts "Method '#{name}' is missing!"
  end
end

p = Person.new
p.unknown_method

Handling method_missing Dynamically

You can use method_missing to define flexible method handling.

class DynamicPerson
  def method_missing(name, *args)
    if name.to_s.start_with?('say_')
      puts "You asked me to say: #{name.to_s.split('_')[1..-1].join(' ')}"
    else
      super
    end
  end
end

p = DynamicPerson.new
p.say_hello_world

Overriding respond_to_missing?

To properly handle method checks, override respond_to_missing?.

class DynamicPerson
  def method_missing(name, *args)
    if name.to_s.start_with?('say_')
      puts "Saying: #{name.to_s.split('_')[1..-1].join(' ')}"
    else
      super
    end
  end
  
  def respond_to_missing?(name, include_private = false)
    name.to_s.start_with?('say_') || super
  end
end

p = DynamicPerson.new
puts p.respond_to?(:say_hello)

Using method_missing for Dynamic Getters and Setters

You can create dynamic attributes using method_missing.

class DynamicPerson
  def initialize
    @data = {}
  end
  
  def method_missing(name, *args)
    attribute = name.to_s.sub(/=$/, '')
    if name.to_s.end_with?('=')
      @data[attribute] = args.first
    else
      @data[attribute]
    end
  end
end

p = DynamicPerson.new
p.name = 'Alice'
puts p.name

Security Risks of method_missing

Using method_missing improperly can introduce risks:

  • Unexpected behavior when handling unknown methods.
  • Potential performance issues due to excessive method lookups.
  • Security vulnerabilities if combined with user input.

Dynamic Methods Recap

Congratulations!

You have learned how to define and call methods dynamically in Ruby.

Next, you will explore open classes and monkey patching.

Dynamic Method Definition — illustration 10
Free to start

Learn Ruby with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
29
Lessons
109

Frequently Asked Questions

Is the “Dynamic Method Definition” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Dynamic Method Definition”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.

What will I learn in “Dynamic Method Definition”?

Learn how to define and call methods dynamically using define_method and method_missing. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Dynamic Method Definition” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

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