Skip to content

Instantly share code, notes, and snippets.

@wycats
Created January 24, 2010 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save wycats/284931 to your computer and use it in GitHub Desktop.
Save wycats/284931 to your computer and use it in GitHub Desktop.
# file1
namespace Yehuda
class String
def printout
puts self
end
end
# file1 after recompilation
class String
define_method("__ns__::Yehuda::printout") do
puts self
end
end
# file2
namespace Rails
class String
def printout
puts "Rails: #{self}"
end
end
# file2 after recompilation
class String
define_method("__ns__::Rails::printout") do
puts "Rails: #{self}"
end
end
# file3
use Yehuda
use Rails
"Yehuda".printout
# file2 after recompilation
tmp = "Yehuda"
if meth = callsite.get(String, :printout)
tmp.send(meth)
end
if tmp.respond_to?("__ns__::Yehuda::printout")
callsite.set(String, :printout, "__ns__::Yehuda::printout")
tmp.send("__ns__::Yehuda::printout")
elsif tmp.respond_to?("__ns__::Rails::printout")
callsite.set(String, :printout, "__ns__::Rails::printout")
tmp.send("__ns__::Rails::printout")
else
callsite.set(String, :printout, "printout")
tmp.printout
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment