From: Robert Klemme Date: 2013-02-12T21:29:20+09:00 Subject: Re: Select "columns" from multidimensional array? On Tue, Feb 12, 2013 at 1:19 PM, Joel Pearson wrote: > I've decided to inherit from array after all, since all I want to do > with this is extend support for multidimensional arrays, but without > overwriting any of Array's methods. I usually do not engage in predictions since I don't have a crystal ball but in this case I'll say: you won't get happy with that approach. For example, anybody can override header values or complete rows / columns violating your class's idea of internal state. > Anyway, the obstacle I've hit is one I can avoid, but I was wondering > whether I'm doing something wrong, or whether there's a nice Rubyish way > around this. Here's a simplified version to demonstrate the issue: > > ___________________ > class Excel_Sheet > def initialize( val=[] ) > val = %w(A1 B1 C1 A2 B2 C2 A3 B3 C3).each_slice(3).to_a if val == > 'test' > super ( val ) > end > > def skip_headers > block_given? ? ( [ self[0] ] + yield( self[1..-1] ) ) : ( > self[1..-1] ) > end What is this supposed to do? Ah, I think I see. I'd probably name it differently, i.e. each_data_cell or something. > def filter( header, regex ) > idx = self[0].index header > skip_headers { |xl| xl.select { |ar| ar[idx] =~ regex } } > end > > end That combines too much logic in one method IMHO. I'd rather select a row based on header and then I would use #select on that. > So in short, my question is how can I return my class type after using > Array's methods on my child-class? Do you mean as return value from #map and the like? Well, you can't without overriding all methods with this approach, I'm afraid. That's one of the reasons why this approach does not work well. :-) Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/