Robert K. wrote in post #1096452:
anybody can override header values or complete
rows / columns violating your class’s idea of internal state.
This class only gets added into scripts, and I’m the only one who knows
Ruby where I work, so I don’t really see this being a problem yet.
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.
This basically allows me to make summaries or edits within the data
itself without having to worry about the headers, then it tacks the
headers back on again once I’ve made the changes. The switch is just so
I can rip the headers off data and also read my code back later and see
what it’s doing.
def filter( header, regex )
idx = self[0].index header
skip_headers { |xl| xl.select { |ar| ar[idx] =~ regex } }
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.
In this case, I’m filtering the data like Excel does. This means I’m
keeping all of the columns, but only specific rows based on the values
in a single column.
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.
You’re probably thinking in terms of classes which are constantly active
as objects accessible to multiple users, whereas I’m just using this
class to make scripts easier to write.
My usage in this case is:
- Run a report which returns a table of data
- Filter the data on a set of criteria
- Output the results to Excel
- Exit
Using the class I’ve built I can easily pare down the data to the
important sections, I’ve added other handy methods suitable to a
multidimensional array such as “find” which returns the header and row,
“to_s” which converts to TSV format, and several more. It saves me
rewriting the same stuff over and over.
It’s great when I want to build excel-style tabs out of the data, like
this little snippet:
@data[:EXT] = @WIP_data.filter ‘Job Status’, /External Repair/
@data[:Awaiting_Parts] = @WIP_data.filter ‘Job Status’, /Awaiting Parts/
@data.each { |name, data| @data[:Summary].push [ name.to_str, (
data.length -1 ) ] }
reporter.output_to_excel_tabs @data, filename
Ok, in that case I’m happy with an array return, but there are plenty of
sequential events where I’d like to have my own class type returned. Is
passing it through the initializer again really the best way to do that?