Skip to content

Instantly share code, notes, and snippets.

@mzdravkov
Last active December 23, 2015 22:18
Show Gist options
  • Save mzdravkov/6701867 to your computer and use it in GitHub Desktop.
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.
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