Skip to content

Instantly share code, notes, and snippets.

@jsvnm
Created November 17, 2011 16:02
Show Gist options
  • Save jsvnm/1373529 to your computer and use it in GitHub Desktop.
Save jsvnm/1373529 to your computer and use it in GitHub Desktop.
from singleton to instance method
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
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