Created
November 8, 2012 10:56
-
-
Save edvardm/4038157 to your computer and use it in GitHub Desktop.
map helper
This file contains 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
module BlockHelper | |
def map_args(method_name) | |
map(&method(method_name.to_sym)) | |
end | |
def each_args(method_name) | |
each(&method(method_name.to_sym)) | |
end | |
end | |
Array.send(:include, BlockHelper) | |
Hash.send(:include, BlockHelper) | |
def double(n); 2*n; end | |
[1, 2, 3].map_args(:double) | |
[2, 4, 6] | |
[1, 2, 3].each_args(:puts) | |
1 | |
2 | |
3 | |
def double_val((k, v)); 2*v; end => nil | |
{'foo' => 42}.map_args(:double_val) | |
[84] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment