1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

* lib/matrix.rb: New methods Matrix#each, #each_with_index, and

include Enumerable [ruby-core:28400]

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@27158 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
marcandre 2010-04-01 18:04:14 +00:00
parent af6e31b344
commit 277cb36b21

View file

@ -65,6 +65,8 @@ end
# * <tt> #column(j) </tt>
# * <tt> #collect </tt>
# * <tt> #map </tt>
# * <tt> #each </tt>
# * <tt> #each_with_index </tt>
# * <tt> #minor(*param) </tt>
#
# Properties of a matrix:
@ -104,6 +106,7 @@ class Matrix
@RCS_ID='-$Id: matrix.rb,v 1.13 2001/12/09 14:22:23 keiju Exp keiju $-'
# extend Exception2MessageMapper
include Enumerable
include ExceptionForMatrix
# instance creations
@ -339,6 +342,42 @@ class Matrix
end
alias map collect
#
# Yields all elements of the matrix, starting with those of the first row,
# or returns an Enumerator is no block given
# Matrix[ [1,2], [3,4] ].each { |e| puts e }
# # => prints the numbers 1 to 4
#
def each(&block) # :yield: e
return to_enum(:each) unless block_given?
@rows.each do |row|
row.each(&block)
end
self
end
#
# Yields all elements of the matrix, starting with those of the first row,
# along with the row index and column index,
# or returns an Enumerator is no block given
# Matrix[ [1,2], [3,4] ].each_with_index do |e, row, col|
# puts "#{e} at #{row}, #{col}"
# end
# # => 1 at 0, 0
# # => 2 at 0, 1
# # => 3 at 1, 0
# # => 4 at 1, 1
#
def each_with_index(&block) # :yield: e, row, column
return to_enum(:each_with_index) unless block_given?
@rows.each_with_index do |row, row_index|
row.each_with_index do |e, col_index|
yield e, row_index, col_index
end
end
self
end
#
# Returns a section of the matrix. The parameters are either:
# * start_row, nrows, start_col, ncols; OR