Rubymethod&constant
lookup
Index
1. Module
2. Ruby Method Lookup
3. Module use cases
4. Ruby Constant Lookup
Module
module BaseInfo
def title
end
def isbn
end
end
ModuleisnotClassinmanyways
/*WRONG*/
module BaseInfo
end
@base = BaseInfo.new
/*WRONG*/
module BaseInfo < Base
end
/*CORRECT*/
module BaseInfo
end
class ClassicNovel
include BaseInfo
end
IncludingModuleintoaClass
module BaseInfo
end
class ClassicNovel < Novel
include BaseInfo
end
Superclass linkedlist
ClassicNovel
Novel
Object
Included
class
BaseInfo
copy
RubyMethodLookupAlgorithm
Example
- 1 Class, 1 super class, 1 module
class Novel
attr_accessor :title
attr_accessor :category
end
module BaseInfo
def print
end
end
class ClassicNovel < Novel
include BaseInfo
end
Globalmethodcache
klass defined_class
Fixnum#times Integer#times
Object#puts BasicObject#puts
Multipleinheritance
Include multiple modules
module Status
end
module BaseInfo
def print
end
end
class ClassicNovel < Novel
include BaseInfo
include Status
end
Includeonemoduleintoanother
module Status
end
module BaseInfo
include Status
def print
end
end
class ClassicNovel < Novel
include BaseInfo
end
Usecase1:
- Need to use prepend? (available from ruby 2.0)
Howprependwork
Novel
Status
“Origin” Novel
Object
Move attr_accessor :title to
“Origin” Novel
Usecase2:
Modify a module after including?
Ruby copy structure of module, keep pointer to method table
Usecase3:
Include submodule into included module?
ConstantLookup
the same with method lookup?
- Using lexical scope
- Same algorithm
LexicalScope
ConstantLookupAlgorithm

Ruby method lookup