Skip to content

Instantly share code, notes, and snippets.

@tiagopog
Created January 31, 2015 15:07
Show Gist options
  • Save tiagopog/eee857b95860b736c487 to your computer and use it in GitHub Desktop.
Save tiagopog/eee857b95860b736c487 to your computer and use it in GitHub Desktop.
Monkey Patch example (Ruby).
puts "--- Example 1:"
class Foo
def bar; "bar" end
end
a = Foo.new
puts "a.bar: #{a.bar}"
puts "--- Example 2:"
class Foo
def foobar; "foobar" end
end
b = Foo.new
puts "b.bar: #{b.bar}"
puts "b.foobar: #{b.foobar}"
puts "a.bar: #{a.bar}"
puts "a.foobar: #{a.foobar}"
puts "--- Example 3:"
c = Foo.new
class << c
def foo; "foo" end
end
puts "c.bar: #{c.bar}"
puts "c.foobar: #{c.foobar}"
puts "c.foo: #{c.foo}"
puts "a.bar: #{a.bar}"
puts "a.foobar: #{a.foobar}"
puts "a.foo: #{a.foo}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment