Created
February 28, 2012 01:46
-
-
Save prodis/1928447 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Diferenças de "include" e "extend" na inclusão de módulos
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 Numbers | |
def one | |
puts "1" | |
end | |
def two | |
puts "2" | |
end | |
end | |
module Ordinals | |
def first | |
puts "1st" | |
end | |
def second | |
puts "2nd" | |
end | |
end |
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
class MyClass1 | |
include Numbers | |
extend Ordinals | |
end |
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
m1 = MyClass1.new | |
m1.one # => "1" | |
m1.two # => "2" | |
m1.first # => NoMethodError: undefined method `first' for #<MyClass1:0x035930> | |
m1.second # => NoMethodError: undefined method `second' for #<MyClass1:0x035930> | |
MyClass1.one # => NoMethodError: undefined method `one' for MyClass1:Class | |
MyClass1.two # => NoMethodError: undefined method `two' for MyClass1:Class | |
MyClass1.first # => "1st" | |
MyClass1.second # => "2nd" |
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
class MyClass2 | |
end | |
MyClass2.extend Ordinals | |
MyClass2.first # => "1st" | |
MyClass2.second # => "2nd" | |
m2 = MyClass2.new | |
m2.first # => NoMethodError: undefined method `first' for #<MyClass2:0x10402a8> | |
m2.second # => NoMethodError: undefined method `second' for #<MyClass2:0x10402a8> |
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
MyClass2.include Ordinals # => NoMethodError: private method `include' called for MyClass2: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
class MyClass3 | |
end | |
m3 = MyClass3.new | |
m3.extend Ordinals | |
m3.first # => "1st" | |
m3.second # => "2nd" | |
MyClass3.first # => NoMethodError: undefined method `first' for MyClass3:Class | |
MyClass3.second # => NoMethodError: undefined method `second' for MyClass3:Class | |
MyClass3.new.first # => NoMethodError: undefined method `first' for #<MyClass3:0x1055f04> | |
MyClass3.new.second # => NoMethodError: undefined method `second' for #<MyClass3:0x105bf30> |
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
class MyClass4 | |
end | |
MyClass4.send(:include, Numbers) | |
m4 = MyClass4.new | |
m4.one # => "1" | |
m4.two # => "2" | |
MyClass4.one # => NoMethodError: undefined method `one' for MyClass4:Class | |
MyClass4.two # => NoMethodError: undefined method `two' for MyClass4:Class |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito legal esse Gist!