Skip to content

Instantly share code, notes, and snippets.

@spalenza
Created February 7, 2013 00:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spalenza/4727354 to your computer and use it in GitHub Desktop.
Save spalenza/4727354 to your computer and use it in GitHub Desktop.
Ruby create methods with define_method
# normal
class Library
attr_accessor :games
def each(&block)
games.each(&block)
end
def map(&block)
games.map(&block)
end
def select(&block)
games.select(&block)
end
end
# define_method
class Library
attr_accessor :games
[:each, :map, :select].each do |method|
define_method method do |&block|
games.send(method, &block)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment