Skip to content

Instantly share code, notes, and snippets.

@apeiros
Created April 1, 2016 12:36
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 apeiros/6428ca06a6c2a8dd60e25c501c07a8de to your computer and use it in GitHub Desktop.
Save apeiros/6428ca06a6c2a8dd60e25c501c07a8de to your computer and use it in GitHub Desktop.
class SliceApplicator
def initialize(target, *slice)
@target = target
@slice = slice
end
def respond_to_missing?(name)
@target.respond_to?(name)
end
def method_missing(name, *args, &block)
slice = @target[*@slice]
result = slice.public_send(name, *args, &block)
@target[*@slice] = slice
result.equal?(slice) ? @target : result
end
end
class Array
def partially_apply(*slice)
SliceApplicator.new(self, *slice)
end
end
ary = [1,2,3,4,5,6,7,8]
ary.partially_apply(0, 4).reject! do |x| x.odd? end
ary # => [2, 4, 5, 6, 7, 8]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment