Method: Matrix#index
- Defined in:
- lib/matrix.rb
#index(*args) ⇒ Object Also known as: find_index
:call-seq:
index(value, selector = :all) -> [row, column]
index(selector = :all){ block } -> [row, column]
index(selector = :all) -> an_enumerator
The index method is specialized to return the index as [row, column] It also accepts an optional selector argument, see #each for details.
Matrix[ [1,2], [3,4] ].index(&:even?) # => [0, 1]
Matrix[ [1,1], [1,1] ].index(1, :strict_lower) # => [1, 0]
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 |
# File 'lib/matrix.rb', line 678 def index(*args) raise ArgumentError, "wrong number of arguments(#{args.size} for 0-2)" if args.size > 2 which = (args.size == 2 || SELECTORS.include?(args.last)) ? args.pop : :all return to_enum :find_index, which, *args unless block_given? || args.size == 1 if args.size == 1 value = args.first each_with_index(which) do |e, row_index, col_index| return row_index, col_index if e == value end else each_with_index(which) do |e, row_index, col_index| return row_index, col_index if yield e end end nil end |