Skip to content

Instantly share code, notes, and snippets.

@pricees
Created July 24, 2012 07:03
Show Gist options
  • Save pricees/3168496 to your computer and use it in GitHub Desktop.
Save pricees/3168496 to your computer and use it in GitHub Desktop.
Module Fun and Pythony Methods In Ruby
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