mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2025-08-10 00:36:48 +02:00
Added module extension and inclusion examples
Also added examples for callbacks made when modules are included and extended.
This commit is contained in:
@@ -403,4 +403,55 @@ end
|
|||||||
Human.bar # 0
|
Human.bar # 0
|
||||||
Doctor.bar # nil
|
Doctor.bar # nil
|
||||||
|
|
||||||
|
module ModuleExample
|
||||||
|
def foo
|
||||||
|
'foo'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
# Including modules binds the methods to the object instance
|
||||||
|
# Extending modules binds the methods to the class instance
|
||||||
|
|
||||||
|
class Person
|
||||||
|
include ModuleExample
|
||||||
|
end
|
||||||
|
|
||||||
|
class Book
|
||||||
|
extend ModuleExample
|
||||||
|
end
|
||||||
|
|
||||||
|
Person.foo # => NoMethodError: undefined method `foo' for Person:Class
|
||||||
|
Person.new.foo # => 'foo'
|
||||||
|
Book.foo # => 'foo'
|
||||||
|
Book.new.foo # => NoMethodError: undefined method `foo'
|
||||||
|
|
||||||
|
# Callbacks when including and extending a module are executed
|
||||||
|
|
||||||
|
module ConcernExample
|
||||||
|
def self.included(base)
|
||||||
|
base.extend(ClassMethods)
|
||||||
|
base.send(:include, InstanceMethods)
|
||||||
|
end
|
||||||
|
|
||||||
|
module ClassMethods
|
||||||
|
def bar
|
||||||
|
'bar'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module InstanceMethods
|
||||||
|
def qux
|
||||||
|
'qux'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class Something
|
||||||
|
include ConcernExample
|
||||||
|
end
|
||||||
|
|
||||||
|
Something.bar # => 'bar'
|
||||||
|
Something.qux # => NoMethodError: undefined method `qux'
|
||||||
|
Something.new.bar # => NoMethodError: undefined method `bar'
|
||||||
|
Something.new.qux # => 'qux'
|
||||||
```
|
```
|
||||||
|
Reference in New Issue
Block a user