Created
July 24, 2012 07:03
-
-
Save pricees/3168496 to your computer and use it in GitHub Desktop.
Module Fun and Pythony Methods In Ruby
This file contains 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 Greeting | |
extend self | |
def say_hello(greetable = self) | |
puts greetable.greeting | |
end | |
end | |
class Person | |
attr_accessor :greeting | |
end | |
# | |
# Pass the object as a greetable to the module | |
# | |
person = Person.new | |
person.greeting = "Bienvenido, a Miami" | |
Greeting.say_hello person # "Bienvenido, a Miami" | |
# | |
# Include the Greeting module and | |
# use :say_hello as an instance method | |
# | |
person.respond_to? :say_hello #false | |
Person.send :include, Greeting | |
person.say_hello # "Bienvenido, a Miami" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment