Created
May 9, 2012 16:31
-
-
Save joshsusser/2646288 to your computer and use it in GitHub Desktop.
Challenge: implement enumerable methods using #inject instead of #each.
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
def collect(&block) | |
inject([]) { |memo, obj| memo << block.call(obj) } | |
end | |
def detect(&block) | |
inject(nil) { |memo, obj| memo ||= obj if block.call(obj) } | |
end | |
def reject(&block) | |
inject([]) { |memo, obj| block.call(obj) ? memo : memo << obj } | |
end | |
def select(&block) | |
inject([]) { |memo, obj| block.call (obj) ? memo << obj : memo } | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment