Created
August 18, 2010 15:28
-
-
Save nusco/535134 to your computer and use it in GitHub Desktop.
Spell: Pattern Dispatch
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: Pattern Dispatch | |
# ======================= | |
# Select which methods to call based on their names. | |
$x = 0 | |
class C | |
def my_first_method | |
$x += 1 | |
end | |
def my_second_method | |
$x += 2 | |
end | |
end | |
obj = C.new | |
obj.methods.each do |m| | |
obj.send(m) if m.to_s =~ /^my_/ | |
end | |
$x # => 3 | |
# 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