Created
August 7, 2019 05:07
-
-
Save qubitrenegade/d1db5ebafc3706594d4a21abe9815e95 to your computer and use it in GitHub Desktop.
Prepend example of "safe" monkeypatching
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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