Created
December 20, 2013 09:19
-
-
Save markmeeus/8052351 to your computer and use it in GitHub Desktop.
Ever wish there was a symol.to_proc style way to call methods with parameters?
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
class Array | |
class ArrayProcWrapper | |
attr_accessor :ary | |
def to_proc | |
lambda { |obj| obj.send self.ary.first, *self.ary[1..-1]} | |
end | |
end | |
def to_proc_wrapper | |
w = ArrayProcWrapper.new | |
w.ary = self | |
w | |
end | |
alias :tpw :to_proc_wrapper | |
end | |
#now you can do this | |
array = ["Marks awesome gist", "is really", "awesome"] | |
array.map(&[:split, ' '].tpw) #splits every string by spaces | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment