0Pricing
Ruby Academy · Lesson

Open Classes and Monkey Patching

Explore how Ruby allows modifying existing classes and adding new functionality.

Open Classes and Monkey Patching is a free Ruby Academy lesson on CoddyKit — lesson 3 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

Open Classes and Monkey Patching

Ruby allows modifying existing classes at runtime.

This feature, called 'Open Classes', enables developers to extend or change behavior dynamically.

Open Classes and Monkey Patching — illustration 1

2

What are Open Classes?

In Ruby, you can reopen and modify existing classes.

This allows adding new methods to built-in or user-defined classes.

3

Adding Methods to an Existing Class

Simply redefine a class and add new methods.

class String
  def shout
    self.upcase + '!!!'
  end
end

puts 'hello'.shout

4

Overriding Methods (Monkey Patching)

You can override existing methods in Ruby classes.

class String
  def reverse
    'Reversed method overridden!'
  end
end

puts 'hello'.reverse

5

Preserving the Original Method with super

Use super to keep the original method’s behavior.

class String
  def reverse
    super + ' (custom behavior added)'
  end
end

puts 'hello'.reverse

6

Use Cases for Open Classes

Open classes are useful in various scenarios:

  • Extending built-in classes with additional methods.
  • Enhancing third-party libraries.
  • Improving readability and maintainability.

7

Risks of Monkey Patching

Monkey patching can be dangerous if not used carefully.

  • Unexpected behavior when modifying core classes.
  • Potential conflicts with third-party libraries.
  • Harder debugging and maintainability.

8

9

Alternatives to Monkey Patching

Instead of modifying global behavior, consider using refinements.

Refinements allow modifying classes in a limited scope.

module StringExtensions
  refine String do
    def shout
      upcase + '!!!'
    end
  end
end

using StringExtensions
puts 'hello'.shout

10

Congratulations!

You have learned how to modify existing classes using Open Classes and Monkey Patching.

Next, you will explore how to use send and eval for dynamic method execution.

Open Classes and Monkey Patching — illustration 10

Frequently asked questions

Is the “Open Classes and Monkey Patching” lesson free?

Yes — the full text of “Open Classes and Monkey Patching” 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 “Open Classes and Monkey Patching”?

Explore how Ruby allows modifying existing classes and adding new functionality. 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 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Open Classes and Monkey Patching” 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