Created
November 20, 2012 07:38
-
-
Save rewinfrey/4116604 to your computer and use it in GitHub Desktop.
to_proc & map
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
def meth(a, b, c) | |
[c, b, a] | |
end | |
ary = [1, 2, 3] | |
meth(*ary) # => [3, 2, 1] | |
class Symbol | |
def to_proc2 | |
Proc.new { |element| element.send self } | |
end | |
end | |
class Array | |
def to_proc | |
method_name, *args = self | |
Proc.new { |obj| obj.send method_name, *args } | |
end | |
def to_proc2 | |
method_name = first | |
args = self[1..-1] | |
Proc.new { |obj| obj.send method_name, *args } | |
end | |
end | |
:upcase.to_proc2.call("abc") # => "ABC" | |
%w[abc def ghi].map(&:upcase) # => ["ABC", "DEF", "GHI"] | |
%w[abc def ghi].map(&[:split, ""]) # => [["a", "b", "c"], ["d", "e", "f"], ["g", "h", "i"]] | |
a, *b = [1, 2, 3] | |
a # => 1 | |
b # => [2, 3] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment