Created
March 20, 2012 03:29
-
-
Save sdeming/2130844 to your computer and use it in GitHub Desktop.
Sometimes it's fun to give any old object an obscure payload. AKA, a parasite.
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
module Parasite | |
def self.infect(object) | |
(class << object; self end).class_eval do | |
include Beasties | |
end | |
end | |
module Beasties | |
def __parasites__ | |
@__parasites__ ||= {} | |
end | |
def set_parasite(name, value) | |
__parasites__[name] = value | |
end | |
def get_parasite(name) | |
__parasites__[name] | |
end | |
end | |
end | |
if __FILE__ == $0 | |
x = "Hello world" | |
y = "Hello world" | |
Parasite::infect x | |
puts x.respond_to? :set_parasite | |
puts y.respond_to? :set_parasite | |
x.set_parasite(:abc, 45) | |
puts x.get_parasite(:abc).inspect | |
Parasite::infect y | |
puts y.get_parasite(:abc).inspect | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment