Skip to content

Instantly share code, notes, and snippets.

@hukl
Created March 24, 2009 17:02
Show Gist options
  • Save hukl/84216 to your computer and use it in GitHub Desktop.
Save hukl/84216 to your computer and use it in GitHub Desktop.
class Object
# The hidden singleton lurks behind everyone
def metaclass
class << self
self
end
end
def meta_eval &blk
metaclass.instance_eval &blk
end
# Adds methods to a metaclass
def meta_def name, &blk
meta_eval { define_method name, &blk }
end
# Defines an instance method within a class
def class_def name, &blk
class_eval { define_method name, &blk }
end
end
class Foo
def self.hello( text )
meta_def :hello do
text
end
end
end
# Same as above but without the fancy helper methods
class Foo
class << self
def hi( text )
class << self; self; end.instance_eval do
define_method :hi do
text
end
end
end
end
end
class Bar < Foo
hello "Jesus is here!"
hi "Just another freak, really!"
end
# There! Individual class methods!
puts Bar.hello
# => Jesus is here!
puts Bar.hi
# => Just another freak, really!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment