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: 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 |
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 Instance Variable | |
# ============================== | |
# Store class-level state in an instance variable of the Class object. | |
class C | |
@my_class_instance_variable = "some value" | |
def self.class_attribute |
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 Macro | |
# ================== | |
# Use a class method in a class definition. | |
class C; end | |
class << C | |
def my_macro(arg) |
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: Clean Room | |
# ================= | |
# Use an object as an environment in which to evaluate a block. | |
class CleanRoom | |
def a_useful_method(x); x * 2; end | |
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: Class Extension | |
# ====================== | |
# Define class methods by mixing a module into a class’s eigenclass | |
# (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: Context Probe | |
# ==================== | |
# Execute a block to access information in an object’s context. | |
class C | |
def initialize | |
@x = "a private instance variable" | |
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: Deferred Evaluation | |
# ========================== | |
# Store a piece of code and its context in a proc or lambda for evaluation later. | |
class C | |
def store(&block) | |
@my_code_capsule = block | |
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: Dynamic Dispatch | |
# ======================= | |
# Decide which method to call at runtime. | |
method_to_call = :reverse | |
obj = "abc" | |
obj.send(method_to_call) # => "cba" |