35

I've noticed that there are two common ways to monkey patch a class in ruby:

Define the new members on the class like so:

class Array
   def new_method
     #do stuff
   end
end

And calling class_eval on the class object:

Array.class_eval do
   def new_method
      #do stuff
   end
end

I'm wondering if there is any difference between the two and whether there are advantages to using one approach over the other?

1

2 Answers 2

63

Honestly, I used to use the 1st form (reopening the class), as it feels more natural, but your question forced me to do some research on the subject and here's the result.

The problem with reopening the class is that it'll silently define a new class if the original one, that you intended to reopen, for some reason wasn't defined at the moment. The result might be different:

  1. If you don't override any methods but only add the new ones and the original implementation is defined (e.g., file, where the class is originally defined is loaded) later everything will be ok.

  2. If you redefine some methods and the original is loaded later your methods will be overridden back with their original versions.

  3. The most interesting case is when you use standard autoloading or some fancy reloading mechanism (like the one used in Rails) to load/reload classes. Some of these solutions rely on const_missing that is called when you reference undefined constant. In that case autoloading mechanism tries to find undefined class' definition and load it. But if you're defining class on your own (while you intended to reopen already defined one) it won't be 'missing' any longer and the original might be never loaded at all as the autoloading mechanism won't be triggered.

On the other hand, if you use class_eval you'll be instantly notified if the class is not defined at the moment. In addition, as you're referencing the class when you call its class_eval method, any autoloading mechanism will have a chance to locate class' definition and load it.

Having that in mind class_eval seems to be a better approach. Though, I'd be happy to hear some other opinion.

2
  • Google is a pretty powerful tool after all =)
    – KL-7
    Apr 26, 2012 at 18:52
  • a "like" for the first sentence :)
    – halfbit
    Sep 5, 2020 at 17:34
12

Scope

One big difference that, I think, KL-7 did not point out is the scope in which your new code will be interpreted:

If you are (re)opening a class to manipulate it, the new code you add will be interpreted in the scope of the (original) class.
If you are using Module#class_eval to manipulate a class, the new code you add will be interpreted in the scope surrounding your call to #class_eval and will NOT be aware of the class-scope. If one does not know, this behavior might be counter-intuitive and lead to hard-to-debug errors.

CONSTANT    = 'surrounding scope'

# original class definition (uses class scope)
class C
  CONSTANT  = 'class scope'

  def fun()  p CONSTANT  end
end
C.new.fun    # prints: "class scope"


# monkey-patching with #class_eval: uses surrounding scope!
C.class_eval do
  def fun()  p CONSTANT  end
end
C.new.fun    # prints: "surrounding scope"


# monkey-patching by re-opening the class: uses scope of class C
class C
  def fun()  p CONSTANT  end
end
C.new.fun    # prints: "class scope"
1
  • Interesting point! Also, as a side-note, I tried a version of this with 3 differently-named functions (fun_a, fun_b, fun_c), and then just a single instance object (only calling C.new once, saving it to a variable, and then calling all 3 methods on it), and indeed it still behaves as indicated here (which I expected, but wanted to confirm for certain).
    – lindes
    Jan 2 at 12:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.