1
0
Fork 0
mirror of https://github.com/pry/pry.git synced 2022-11-09 12:35:05 -05:00

hooks: merge #uniq_keeping_last with #merge! and simplify it

This commit is contained in:
Kyrylo Silin 2015-03-14 09:55:16 +02:00
parent fa400271a1
commit 0bca85da16

View file

@ -57,19 +57,18 @@ class Pry
# Pry::Hooks.new.merge!(hooks)
def merge!(other)
@hooks.merge!(other.dup.hooks) do |key, array, other_array|
uniq_keeping_last(array + other_array, &:first)
temp_hash, output = {}, []
(array + other_array).reverse_each do |pair|
temp_hash[pair.first] ||= output.unshift(pair)
end
output
end
self
end
def uniq_keeping_last(input, &block)
hash, output = {}, []
input.reverse.each{ |i| hash[block[i]] ||= (output.unshift i) }
output
end
private :uniq_keeping_last
# Return a new `Pry::Hooks` instance containing a merge of the contents of two `Pry:Hooks` instances,
# @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
# @return [Pry::Hooks] The new hash.