During the monthly meeting of our code dojo, we were surprised by a
couple
of things in Ruby, so I had a couple of questions I’d like to ask the
community:
- Would it make sense to talk about a Range having a length, as in:
class Range
def length
self.end - self.begin
end
end
- What about a 2 dimensional slice (you want a submatrix, for example)?
def test_extract_submatrix
assert_equal [[1,2,3],[4,5,6],[7,8,9]].extract_submatrix(1…2,1…2)
, [[5,6],[8,9]]
end
class Array
def extract_submatrix(row_range, col_range)
self[row_range].transpose[col_range].transpose
end
end
-
Are the better/more idiomatic ways to do these?
-
Excuse my ignorance, as I’ve yet to use Facets, but are these the
type of
things it adds (and more)? Are they already in there?
Thanks and kind regards,
Sam