Last active
December 15, 2015 17:49
-
-
Save reggieb/5299410 to your computer and use it in GitHub Desktop.
An example of how a class can be replaced with a modified version within a name space.
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 Thing | |
def colour | |
'blue' | |
end | |
def who | |
self.class.to_s | |
end | |
def output(line) | |
puts '--------------------' | |
puts "#{who} on #{line}" | |
puts colour | |
end | |
end | |
module MyExtender | |
class OtherThing < Thing | |
def colour | |
'red' | |
end | |
end | |
OldThing = Thing | |
self.const_set :Thing, OtherThing | |
end | |
module MySpace | |
t = Thing.new | |
t.output(__LINE__) | |
include MyExtender | |
t = Thing.new | |
t.output(__LINE__) | |
t = OldThing.new | |
t.output(__LINE__) | |
end | |
t = Thing.new | |
t.output(__LINE__) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This outputs: