Created
February 7, 2013 00:42
-
-
Save spalenza/4727354 to your computer and use it in GitHub Desktop.
Ruby create methods with define_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
# 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