Created
January 15, 2012 17:16
-
-
Save prodis/1616485 to your computer and use it in GitHub Desktop.
Ruby Fundamental - Módulos como mixin
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 CoolModule | |
def do_something | |
"I did it!" | |
end | |
end | |
class SomeClass | |
include CoolModule | |
end | |
my_class = SomeClass.new | |
my_class.do_something # => "I did it!" |
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 OtherClass | |
include Enumerable, Comparable | |
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
module CoolModule | |
def do_something | |
"I did it!" | |
end | |
end | |
module Other | |
include CoolModule | |
def check_task(task) | |
"#{task}? #{do_something}" | |
end | |
end | |
class SomeClass | |
include Other | |
end | |
my_class = SomeClass.new | |
my_class.do_something # => "I did it!" | |
my_class.check_task("Wash the car") # => "Wash the car? I did it!" |
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 Information | |
attr :title | |
def initialize(title) | |
@title = title | |
end | |
end | |
class Article | |
include Information | |
end | |
article = Article.new("Um título") | |
article.title # => "Um título" |
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 Information | |
attr :title | |
def initialize(title) | |
@title = title | |
end | |
end | |
class Page | |
include Information | |
def initialize(title) | |
@title = "Page: #{super(title)}" | |
end | |
end | |
page = Page.new("Outro título") | |
page.title # => "Page: Outro título" |
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 Information | |
def initialize(title) | |
@information_title = title | |
end | |
def title | |
@information_title | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment