Showing posts with label Include. Show all posts
Showing posts with label Include. Show all posts

Tuesday, January 06, 2009

Rails module include way

# Rails module include way
# 以下方式的代码在rails中源码中的相当多见,其中的self.included(base)方法是一个回调方法,当此module被其他名为base的module(或者class)include的时候触发此方法。通过class_eval,include,extend加入了实例方法和类方法到base中,代码划分得很干净。

module ActionController
module Components
def self.included(base)
base.class_eval do
include InstanceMethods
extend ClassMethods
helper HelperMethods
end
end

module ClassMethods
end

module HelperMethods
end

module InstanceMethods
end
end
end

Monday, January 07, 2008

NOTE SYNTAX OF require/load VS. SYNTAX OF include[R4R page159]

You may have noticed that when you use require or load, you put the name of the item you’re requiring or loading in quotation marks, but with include, you don’t. require and load take strings as their arguments, whereas include takes the name of a module, in the form of a constant. The requirements to require and load are usually literal strings (in quotation marks), but a string in a variable will also work.