0Pricing
Ruby Academy · Lesson

Dynamic Method Definition

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

Dynamic Method Definition is a free Ruby Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

3

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')

4

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

5

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

6

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)

7

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

8

9

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.

10

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

Frequently asked questions

Is the “Dynamic Method Definition” lesson free?

Yes — the full text of “Dynamic Method Definition” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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