From: Marc-Andre Lafortune Date: 2010-03-02T16:52:31+09:00 Subject: [ruby-core:28399] [Bug #2495] Matrix: Vector#each2 should check its argument Issue #2495 has been updated by Marc-Andre Lafortune. Hi, On Mon, Feb 1, 2010 at 10:44 PM, Yusuke ENDOH wrote: > Matz or Keiju, will you give us your opinion? Ist Keiju is even registered on the ruby-core mailing list? I'd like to point out an alternate implementation can use #each instead of #[], and at least avoid the non-sensical results on integers. Example: diff --git a/lib/matrix.rb b/lib/matrix.rb index d452bf4..f9b2f58 100644 --- a/lib/matrix.rb +++ b/lib/matrix.rb @@ -1146,8 +1146,10 @@ class Vector def each2(v) # :yield: e1, e2 Vector.Raise ErrDimensionMismatch if size != v.size return to_enum(:each2, v) unless block_given? - size.times do |i| - yield @elements[i], v[i] + i = 0 + v.each do |elem| + yield @elements[i], elem + i+=1 end self end I'm all for duck typing, as long as we give an idea of what type of "quacking" we're expecting. I would still suggest adding at the beginning of the method: unless v.respond_to?(:size) && v.respond_to?(:each) raise TypeError, "Argument must respond to :size and :each" end ---------------------------------------- http://redmine.ruby-lang.org/issues/show/2495 ---------------------------------------- http://redmine.ruby-lang.org