[前][次][番号順一覧][スレッド一覧]

ruby-changes:13665

From: marcandre <ko1@a...>
Date: Sat, 24 Oct 2009 15:04:51 +0900 (JST)
Subject: [ruby-changes:13665] Ruby:r25450 (trunk): * lib/matrix (minor): Also handle negative arguments like Array#[]

marcandre	2009-10-24 15:04:41 +0900 (Sat, 24 Oct 2009)

  New Revision: 25450

  http://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=rev&revision=25450

  Log:
    * lib/matrix (minor): Also handle negative arguments like Array#[]
      cf [ruby-core:23598]

  Modified files:
    trunk/lib/matrix.rb

Index: lib/matrix.rb
===================================================================
--- lib/matrix.rb	(revision 25449)
+++ lib/matrix.rb	(revision 25450)
@@ -346,22 +346,35 @@
   #     => 9 0 0
   #        0 5 0
   #
+  # Like Array#[], negative indices count backward from the end of the
+  # row or column (-1 is the last element). Returns nil if the starting
+  # row or column is greater than row_size or column_size respectively.
+  #
   def minor(*param)
     case param.size
     when 2
       from_row = param[0].first
-      size_row = param[0].end - from_row
-      size_row += 1 unless param[0].exclude_end?
+      from_row += row_size if from_row < 0
+      to_row = param[0].end
+      to_row += row_size if to_row < 0
+      to_row += 1 unless param[0].exclude_end?
+      size_row = to_row - from_row
       from_col = param[1].first
-      size_col = param[1].end - from_col
-      size_col += 1 unless param[1].exclude_end?
+      from_col += column_size if from_col < 0
+      to_col = param[1].end
+      to_col += column_size if to_col < 0
+      to_col += 1 unless param[1].exclude_end?
+      size_col = to_col - from_col
     when 4
       from_row, size_row, from_col, size_col = param
+      return nil if size_row < 0 || size_col < 0
+      from_row += row_size if from_row < 0
+      from_col += column_size if from_col < 0
     else
       Matrix.Raise ArgumentError, param.inspect
     end
 
-    return nil if from_row > row_size || from_col > column_size
+    return nil if from_row > row_size || from_col > column_size || from_row < 0 || from_col < 0
     rows = @rows[from_row, size_row].collect{|row|
       row[from_col, size_col]
     }

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]