Ruby Academy · Lesson

Open Classes and Monkey Patching

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

Lesson 3 of 410 steps

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

Modifying Classes at Runtime

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

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.

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

Overriding Methods (Monkey Patching)

You can override existing methods in Ruby classes.

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

puts 'hello'.reverse

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

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.

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.

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

Open Classes Recap

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
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 “Open Classes and Monkey Patching” lesson free?

Yes — any 3 lessons of the Ruby Academy learning path, including “Open Classes and Monkey Patching”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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