This file contains hidden or 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: Pattern Dispatch | |
# ======================= | |
# Select which methods to call based on their names. | |
$x = 0 | |
class C | |
def my_first_method |
This file contains hidden or 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: Open Class | |
# ================= | |
# Modify an existing class. | |
class String | |
def my_string_method | |
"my method" | |
end |
This file contains hidden or 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: Nil Guard | |
# ================ | |
# Override a reference to nil with an “or.” | |
x = nil | |
y=x || "avalue" # =>"avalue" | |
# For more information: http://www.pragprog.com/titles/ppmetr/metaprogramming-ruby |
This file contains hidden or 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" |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |