Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using refinements to monkey patch a reversed `reduce` in Ruby
module Consistency
original = Enumerable.instance_method :reduce
refine Enumerable do
# I didn't actually check it against the real impl or docs or anything, but its something like this
alias_method :inject, define_method(:reduce) { |*args, &block|
block = args.pop if args.size == 2
Enumerator.new do |y|
original.bind(self).call(*args) { |l, r| y.yield r, l }
end.each &block
}
end
end
class A
# # it is reversed
%w[a b c].reduce &:+ # => "abc"
using Consistency
%w[a b c].reduce &:+ # => "cba"
# initial value + a symbol
%w[a b c].reduce "-abc", :+ # => "cba-abc"
# initial value + a block
%w[a b c].reduce "-abc", &:+ # => "cba-abc"
# initial value + a symbol + a block (prefers symbol over block)
%w[a b c].reduce("-abc", :+) { |l, r| r + l } # => "cba-abc"
# chainable
%w[a b c].reduce("!").with_index do |(char, str), index|
"#{str} #{index}:#{char}"
end # => "! 0:a 1:b 2:c"
# wrong number of args
%w[a b c].reduce("", :+, 123) # => #<Enumerator: #<Enumerator::Generator:0x00007fbffc983d20>:each>
end
# no effect outside the lexical scope, we're back to normal
%w[a b c].reduce &:+ # => "abc"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.