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: Dynamic Proxy | |
# ==================== | |
# Forward to another object any messages that don’t match a method. | |
class MyDynamicProxy | |
def initialize(target) | |
@target = target | |
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: Flat Scope | |
# ================= | |
# Use a closure to share variables between two scopes. | |
class C | |
def an_attribute | |
@attr | |
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: 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: 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: Kernel Method | |
# ==================== | |
# Define a method in module Kernel to make the method available to all objects. | |
module Kernel | |
def a_method | |
"a kernel 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: Lazy Instance Variable | |
# ============================= | |
# Wait until the first access to initialize an instance variable. | |
class C | |
def attribute | |
@attribute = @attribute || "some value" | |
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: Mimic Method | |
# =================== | |
# Disguise a method as another language construct. | |
def BaseClass(name) | |
name == "string" ? String : Object | |
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: 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: Named Arguments | |
# ====================== | |
# Collect method arguments into a hash to identify them by name. | |
def my_method(args) | |
args[:arg2] | |
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: Namespace | |
# ================ | |
# Define constants within a module to avoid name clashes. | |
module MyNamespace | |
class Array | |
def to_s | |
"my class" |