Created
November 13, 2010 23:33
-
-
Save ged/675747 to your computer and use it in GitHub Desktop.
More cool/stupid Method#to_proc tricks
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
#!/usr/bin/env ruby -wKU | |
### A parasitic class | |
class Parasite | |
### Make all future instances of the Parasite actually call | |
### methods of the +victim+ instead. | |
def self.infest( victim ) | |
victim.methods.each do |m| | |
meth = victim.method( m ) | |
define_method( m, &meth ) | |
end | |
end | |
end | |
a_string = "hey, I'm a string! No, really! I feel kinda funny, though." | |
Parasite.infest( a_string ) | |
p a_string | |
p Parasite.new | |
p a_string.object_id | |
p Parasite.new.object_id | |
p a_string.length | |
p Parasite.new.length | |
p a_string.gsub( /[aeiou]/i, '' ) | |
p Parasite.new.gsub( /[aeiou]/i, '' ) | |
# Output: | |
# "hey, I'm a string! No, really! I feel kinda funny, though." | |
# "hey, I'm a string! No, really! I feel kinda funny, though." | |
# 2151901380 | |
# 2151901380 | |
# 58 | |
# 58 | |
# "hy, 'm strng! N, rlly! fl knd fnny, thgh." | |
# "hy, 'm strng! N, rlly! fl knd fnny, thgh." | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment