Last active
December 23, 2015 22:18
-
-
Save mzdravkov/6701867 to your computer and use it in GitHub Desktop.
Apply method for Ruby's Object, as (maybe?) more clean alternative to long method chain.
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
Object.class_eval do | |
def apply(&block) | |
instance_eval(&block) if block_given? | |
end | |
end | |
arr = [] | |
arr.apply do | |
push [1, 2] | |
p self # => [[1, 2]] | |
push [3, 4] | |
p self # => [[1, 2], [3, 4]] | |
flatten | |
end | |
p arr # => [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment