summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authormarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-10-24 06:04:41 +0000
committermarcandre <marcandre@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2009-10-24 06:04:41 +0000
commite5af8e940b2f399d6eb157c96146f6f6bd950793 (patch)
tree790a34852106fd4a0e524f7dd1e32f77b5be7a03 /lib
parent5d6440c74847135962d87323bbdc71fe342c4c51 (diff)
* lib/matrix (minor): Also handle negative arguments like Array#[]
cf [ruby-core:23598] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@25450 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib')
-rw-r--r--lib/matrix.rb23
1 files changed, 18 insertions, 5 deletions
diff --git a/lib/matrix.rb b/lib/matrix.rb
index d62d1338e6..d14fdef4f7 100644
--- a/lib/matrix.rb
+++ b/lib/matrix.rb
@@ -346,22 +346,35 @@ class Matrix
# => 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]
}