Skip to content

Instantly share code, notes, and snippets.

@kaspth
Created March 18, 2024 13:52
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 kaspth/1b82c9e5c9a0a082dc5e96a8f51fe02a to your computer and use it in GitHub Desktop.
Save kaspth/1b82c9e5c9a0a082dc5e96a8f51fe02a to your computer and use it in GitHub Desktop.
For multiple chains of mutating methods, most likely on Enumerables.
class Tapper < BasicObject
def initialize(object) = @object = object
def method_missing(meth)
@object.tap(&meth)
self
end
end
class Object
def tapped(&) = Tapper.new(self).then { _1.instance_exec(_1, &) }
end
[].tapped { uniq!.reverse! } # For multiple chains of mutating methods
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment