Skip to content

Instantly share code, notes, and snippets.

@qubitrenegade
Created August 7, 2019 05:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qubitrenegade/d1db5ebafc3706594d4a21abe9815e95 to your computer and use it in GitHub Desktop.
Save qubitrenegade/d1db5ebafc3706594d4a21abe9815e95 to your computer and use it in GitHub Desktop.
Prepend example of "safe" monkeypatching
class Foo
def initialize
puts '[*] In Foo#initialize'
@foo = ['Added in intialize in Foo']
end
def foo
puts '[*] In Foo#foo: '
pp @foo
puts "[*] foo.class: #{@foo.class}"
puts '[*] Ending in Foo#Foo'
@foo
end
end
module FooExtensions
attr_accessor :bar
def initialize
super
puts '[*] In FooExtensions#initialize'
@foo << 'Added in Foo Extensions'
@bar = 'bar'
end
def foo
puts '[!] In FooExtensions#foo'
puts "[!] bar: #{bar}"
foo_tmp = super
foo_tmp << 'modifiying return'
end
end
class Foo
prepend FooExtensions
end
puts "Calling foo.new"
foo = Foo.new
puts "Calling foo.foo"
puts foo.foo
puts "Calling foo.bar: #{foo.bar}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment