Created
November 17, 2011 16:02
-
-
Save jsvnm/1373529 to your computer and use it in GitHub Desktop.
from singleton to instance method
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
class Class | |
def method_to_instance(*names) | |
names.each{|name| | |
meth = method(name) | |
self.send(:define_method, name, meth.to_proc) | |
} | |
end | |
end | |
class Bar | |
def self.bar | |
"barbi" | |
end | |
method_to_instance :bar | |
end | |
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
class Class | |
def def_both(meth, &block) | |
[:define_method, :define_singleton_method].each{|d| self.send(d, meth, block)} | |
end | |
end | |
class Foo | |
def_both :bar do | |
"foobar!" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment