Monkey fixes embedded ruby ​​classes in limited areas

I work on internal Ruby DSL and make it as beautiful as possible. I need the monkey to pay the Symbol class and add some operators. I want to be responsible for how I do this, and I would like to limit the scope and lifetime of the patches to a specific block of code. Is there a standard template for this? Here is some kind of pseudo code to show what I think:

class SomeContext
  def self.monkey_patch_region(&block)
    context = SomeContext.new
    context.monkey_patch_Symbol
    context.instance_eval(&block)
    context.unmonkey_patch_Symbol
  end

  # magical method
  def monkey_patch_Symbol
    #...
  end

  # another magical method
  def unmonkey_patch_Symbol
    #...
  end

end

+5
source share
2 answers

I believe that you are looking for ruby refinement . The function landed in a ruby ​​trunk, but it can be returned up to 2.0

+5
source

. . , unmonkey.

UPDATE: mixology , (un) ( extend), ( include), monkey/unmonkey, . , - .

+1

All Articles