From: Sammy Larbi Date: 2007-09-21T21:04:46+09:00 Subject: Re: Idiomatic Ruby for Array#extract / Range#length? ------=_Part_32325_14828507.1190376284072 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Content-Disposition: inline On 9/20/07, Sammy Larbi wrote: > > 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 There's been quite a lot of interesting discussion surrounding my first question, which I truly appreciate and have enjoyed reading for the most part. 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 > > But what about this 2-dimensional slice? Any thoughts on that? There is already Array#transpose that assumes a 2D array, so I think it might fit. Can you think of a better way to do it? 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_32325_14828507.1190376284072--