From: Yusuke ENDOH Date: 2010-01-28T00:41:21+09:00 Subject: [ruby-core:27901] Re: [Bug #2495] Matrix: Vector#each2 should check its argument Hi, 2009/12/19 Marc-Andre Lafortune : > Bug #2495: Matrix: Vector#each2 should check its argument > http://redmine.ruby-lang.org/issues/show/2495 > > Author: Marc-Andre Lafortune > Status: Open, Priority: Normal > Assigned to: Keiju Ishitsuka, Category: lib, Target version: 1.9.2 > ruby -v: ruby 1.9.2dev (2009-12-19 trunk 26121) [x86_64-darwin10.2.0] > > $ rubydev -r matrix -e 'p Vector[*1..4].each2(nil){|x, y| p "#{x}, #{y}"}' > /usr/local/rubydev/lib/ruby/1.9.1/matrix.rb:1149:in `each2': undefined method `size' for nil:NilClass (NoMethodError) > > $ rubydev -r matrix -e 'p Vector[*1..4].each2(42){|x, y| p "#{x}, #{y}"}' > /usr/local/rubydev/lib/ruby/1.9.1/matrix.rb:1149:in `each2': Vector dimension mismatch (ExceptionForMatrix::ErrDimensionMismatch) > � � � �from -e:1:in `
' > > $ rubydev -r matrix -e 'p Vector[*1..8].each2(42){|x, y| p "#{x}, #{y}"}' > "1, 0" > "2, 1" > "3, 0" > "4, 1" > "5, 0" > "6, 1" > "7, 0" > "8, 0" Agreed. diff --git a/lib/matrix.rb b/lib/matrix.rb index e6f5fe1..3a8c76a 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1144,6 +1144,9 @@ class Vector # Iterate over the elements of this vector and +v+ in conjunction. # def each2(v) # :yield: e1, e2 + unless v.is_a?(Array) || v.is_a?(Vector) + Vector.Raise TypeError, v.class, "Array or Vector" + end Vector.Raise ErrDimensionMismatch if size != v.size return to_enum(:each2, v) unless block_given? size.times do |i| -- Yusuke ENDOH