Issue #2495 has been updated by Marc-Andre Lafortune.

Status changed from Rejected to Open

Hi

On Sun, Jan 31, 2010 at 10:12 AM, Yusuke Endoh <redmine / ruby-lang.org> wrote:
> According to [ruby-dev:40237] and [ruby-dev:40267], it is the fate of
> duck typing and compatibility.

Thank you for relaying this discussion.

I am all in favor of duck typing. I believe there is a solution here, which would be to insure that the argument responds to :each (or to :to_a.) Indeed, the name of the method is :each2, so I feel it is quite reasonable to request that the argument responds to :each. This would solve the issue for both Integer and String.

Finally, I feel it would be superior to raise a TypeError instead of a NoMethodError in a case where duck typing fails, in a similar fashion to "$stdout = nil" which raises TypeError: $stdout must have write method, NilClass given

So I'd recommend something like:

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.respond_to?(:each) && v.respond_to?(:size) && v.respond_to?(:[])
+      raise TypeError, "Argument should be array-like"
+    end
     Vector.Raise ErrDimensionMismatch if size != v.size
     return to_enum(:each2, v) unless block_given?
     size.times do |i|


----------------------------------------
http://redmine.ruby-lang.org/issues/show/2495

----------------------------------------
http://redmine.ruby-lang.org