Code Golf!

This commit is contained in:
Conrad Irwin 2012-01-11 22:35:16 -08:00
parent 2fdfbad039
commit d2f5725537
1 changed files with 8 additions and 9 deletions

View File

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