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 |
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: Class Extension | |
# ====================== | |
# Define class methods by mixing a module into a class’s singleton class | |
# (a special case of Object Extension - http://gist.github.com/534667). | |
class C; end | |
module M |
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: Singleton Method | |
# ======================= | |
# Define a method on a single object. | |
obj = "abc" | |
class << obj | |
def my_singleton_method |
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: Monkeypatch | |
# ================== | |
# Change the features of an existing class. | |
"abc".reverse # => "cba" | |
class String | |
def reverse |
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: Argument Array | |
# ===================== | |
# Collapse a list of arguments into an array. | |
def my_method(*args) | |
args.map {|arg| arg.reverse } | |
end |
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: Around Alias | |
# =================== | |
# Call the previous, aliased version of a method from a redefined method. | |
class String | |
alias :old_reverse :reverse | |
def reverse |
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: Ghost Method | |
# =================== | |
# Respond to a message that doesn’t have an associated method. | |
class C | |
def method_missing(name, *args) | |
name.to_s.reverse | |
end |
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: Blank Slate | |
# =================== | |
# Remove methods from an object to turn them into Ghost Methods (http://gist.github.com/534776). | |
class C | |
def method_missing(name, *args) | |
"a Ghost Method" | |
end |
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: Hook Method | |
# ================== | |
# Override a method to intercept object model events. | |
$INHERITORS = [] | |
class C | |
def self.inherited(subclass) |
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: Class Extension Mixin | |
# ============================ | |
# Enable a module to extend its includer through a Hook Method (http://gist.github.com/534994). | |
module M | |
def self.included(base) | |
base.extend(ClassMethods) | |
end |
OlderNewer