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

Code Golf!

This commit is contained in:
Conrad Irwin 2012-01-11 22:35:16 -08:00
parent 2fdfbad039
commit d2f5725537

View file

@ -37,18 +37,17 @@ class Pry
end end
def merge_arrays(array1, array2) def merge_arrays(array1, array2)
keys = array1.map {|(k,v)| k} uniq_keeping_last(array1 + array2, &:first)
hash = {}
array1.each{|(k,v)| hash[k]=v}
keys.push(*array2.map {|(k,v)| k})
array2.each{|(k,v)| hash[k]=v}
result = []
keys.uniq!
keys.each{|k| result.push([k,hash[k]]) }
result
end end
private :merge_arrays private :merge_arrays
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, # 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 # @param [Pry::Hooks] other The `Pry::Hooks` instance to merge
# @return [Pry::Hooks] The new hash. # @return [Pry::Hooks] The new hash.