Skip to content

Instantly share code, notes, and snippets.

@sdeming
Created March 20, 2012 03:29
Show Gist options
  • Save sdeming/2130844 to your computer and use it in GitHub Desktop.
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.
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