Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Ruby Fundamental - Módulos como mixin
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!"
class OtherClass
include Enumerable, Comparable
end
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!"
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"
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"
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