Created
September 3, 2014 08:42
-
-
Save ggPeti/bed175adf84dcc4529cd to your computer and use it in GitHub Desktop.
Arrow-like proc handling in Ruby
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 Object | |
def itself | |
self | |
end | |
%i[> >> * &].each { |method| define_method(method) { |*args| to_proc.send method, *args } } | |
end | |
class Proc | |
def > other | |
-> *xs { other.to_proc.(*self.(*xs)) } | |
end | |
def >> other | |
-> *xs { other.to_proc.(self.(*xs)) } | |
end | |
def * other | |
-> *xs, x { [*self.(*xs), other.to_proc.(x)] } | |
end | |
def & other | |
-> *xs { [*self.(*xs), other.to_proc.(*xs)] } | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment