Skip to content

Instantly share code, notes, and snippets.

@mmiliaus
Created April 22, 2011 09:46
Show Gist options
  • Save mmiliaus/936361 to your computer and use it in GitHub Desktop.
Save mmiliaus/936361 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
* instance_eval, class_eval, module_eval
* instance_eval, evaluates code on instance level:
class Bongas
class << self
def nu_kazka
puts "Nu Kazka Bongas"
end
end
end
Bongas.instance_eval { nu_kazka } # -> "Nu Kazka Bongas"
* define_method - creates only instance methods definitions in the class
module Merb::Cache::CacheMixin
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
def cache(*actions)
# ...
#Merb::Cache::CacheMixin acts both as a mixin and as a Namespace (41)
#for an inner module. This inner module, appropriately named Class-
#Methods, defines Class Macros such as cache( ). When you include
#CacheMixin, you trigger a chain of events:
# * Ruby calls a Hook Method (181): the included( ) method.
# * The hook turns back to the including class (which is sometimes
#called the inclusor, or the base in this case) and extends it with
#the ClassMethods module.
# * The extend( ) method includes the methods from ClassMethods in
#the inclusor’s eigenclass.
* In a class or module definition, self is the class or module object.
* At the time the method definition is executed, the most you can say is that self inside this method will be some future object that has access to this method.
* self inside a singleton method (a class method, in this case) is the object whose singleton method it is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment