Last active
September 5, 2015 18:54
-
-
Save nusco/534667 to your computer and use it in GitHub Desktop.
Spell: Object Extension
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
# ======================= | |
# Spell: Object Extension | |
# ======================= | |
# Define Singleton Methods by mixing a module into an object’s singleton class. | |
obj = Object.new | |
module M | |
def my_method | |
'a singleton method' | |
end | |
end | |
class << obj | |
include M | |
end | |
obj.my_method # => "a singleton method" | |
# For more information: http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment