Skip to content

Instantly share code, notes, and snippets.

@CyrusNuevoDia
Last active November 3, 2015 15:47
Show Gist options
  • Save CyrusNuevoDia/a8eb0eebd1e6d08d9cfb to your computer and use it in GitHub Desktop.
Save CyrusNuevoDia/a8eb0eebd1e6d08d9cfb to your computer and use it in GitHub Desktop.
class Chain < Struct.new(:value)
def method_missing(method, *args, &block)
next_value = self.value.public_send(method, *args, &block)
self.value = next_value unless method.to_s[-1] == "!"
self
end
end
# Now you can chain bang methods (or regular methods)!
Chain.new([1, 2, 3, 4, 5]).reject!(&:odd?).reverse!.value # => [4, 2]
@mikeroher
Copy link

Return self after the if/else

@CyrusNuevoDia
Copy link
Author

Fixed 👍 :shipit:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment