Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Spell: Pattern Dispatch
# =======================
# 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