Skip to content

Instantly share code, notes, and snippets.

@ggPeti
Created September 3, 2014 08:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggPeti/bed175adf84dcc4529cd to your computer and use it in GitHub Desktop.
Save ggPeti/bed175adf84dcc4529cd to your computer and use it in GitHub Desktop.
Arrow-like proc handling in Ruby
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