Elegant monkeypatching plus a core library of extensions to the Ruby standard library
= monkey
Monkey is a smart, scoped monkeypatching library. You can define monkeypatches
via an intuitive DSL, and apply them only to scoped blocks, assured that your
changes won’t leak out to objects outside your scope.
== Usage
=== Monkeypatching objects
Monkey.see(Object) do
def metaclass
class << self; self; end
end
end
Monkey.patch(Object, :metaclass) do
“foo”.metaclass # => #<Class:#String:0x1016d7570>
end
“foo”.metaclass # => NoMethodError
Monkey.patch(Object, :metaclass)
“foo”.metaclass # => #<Class:#String:0x1016d7570>
=== Monkeypatching classes
Monkey.see(Object.metaclass) do
def foo
Kernel.rand
end
def bar
"hello!"
end
end
Monkey.patch(Object.metaclass, :foo, :bar) do
Object.foo # => 0.571499912853919
Object.bar # => “hello!”
end
=== Monkeypatching instances
Monkey.see(Object) do
def metaclass
class << self; self; end
end
end
a = “foo”
Monkey.patch(a, :metaclass) { a.metaclass }