Logo

Programming-Idioms

History of Idiom 20 > diff from v47 to v48

Edit summary for version 48 by steenslag:
[Ruby] conform to given variable name m

Version 47

2019-02-02, 03:06:18

Version 48

2019-06-17, 22:37:52

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Idiom #20 Return two values

Implement a function search which looks for item x in a 2D matrix m.
Return indices i, j of the matching cell.
Think of the most idiomatic way in the language to return the two values at the same time.

Code
def search(matrix, x)
  matrix.each_with_index do |row, i|
    row.each_with_index do |value, j|
      return i, j if value == x
    end
  end
  nil
end
Code
def search(m, x)
  m.each_with_index do |row, i|
    row.each_with_index do |value, j|
      return i, j if value == x
    end
  end
  nil
end