RosettaCodeData/Task/Dot-product/Ruby/dot-product-2.rb

9 lines
211 B
Ruby
Raw Permalink Normal View History

2013-04-10 16:57:12 -07:00
class Array
def dot_product(other)
raise "not the same size!" if self.length != other.length
self.zip(other).inject(0) {|dp, (a, b)| dp += a*b}
end
end
p [1, 3, -5].dot_product [4, -2, -1] # => 3