Skip to content

Instantly share code, notes, and snippets.

@nusco
Created August 18, 2010 15:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save nusco/535134 to your computer and use it in GitHub Desktop.
Save nusco/535134 to your computer and use it in GitHub Desktop.
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