Skip to content

Instantly share code, notes, and snippets.

@reggieb
Last active December 15, 2015 17:49
Show Gist options
  • Save reggieb/5299410 to your computer and use it in GitHub Desktop.
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.
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__)
@reggieb
Copy link
Author

reggieb commented Apr 3, 2013

This outputs:

--------------------
Thing on 32
blue
--------------------
MyExtender::OtherThing on 37
red
--------------------
Thing on 40
blue
--------------------
Thing on 45
blue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment