Created
April 22, 2011 09:46
-
-
Save mmiliaus/936361 to your computer and use it in GitHub Desktop.
Ruby Metaprogramming
This file contains hidden or 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
* 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 |
This file contains hidden or 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
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. |
This file contains hidden or 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
* 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