Skip to content

Instantly share code, notes, and snippets.

@rewinfrey
Created November 20, 2012 07:38
Show Gist options
  • Save rewinfrey/4116604 to your computer and use it in GitHub Desktop.
Save rewinfrey/4116604 to your computer and use it in GitHub Desktop.
to_proc & map
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