Last active
November 3, 2015 15:47
-
-
Save CyrusNuevoDia/a8eb0eebd1e6d08d9cfb to your computer and use it in GitHub Desktop.
This file contains 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 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] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Return self after the if/else