From: Sammy Larbi Date: 2007-09-20T21:28:15+09:00 Subject: Idiomatic Ruby for Array#extract / Range#length? ------=_Part_27336_30112547.1190291295390 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline 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: 1) Would it make sense to talk about a Range having a length, as in: class Range def length self.end - self.begin end end 2) 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 3) Are the better/more idiomatic ways to do these? 4) 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 ------=_Part_27336_30112547.1190291295390--