On Tue, Feb 12, 2013 at 2:06 PM, Joel Pearson <[email protected]> wrote: > Robert Klemme 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.
I think you misunderstand OO. The point of OO is to build abstractions of real world phenomena which behave in certain ways. That does not have to do with how many people use the code; proper OO abstractions help you even if you are the only one working with the code because they encapsulate specific behavior and you as a user of classes do not need to worry any longer about internals. By thinking in proper abstractions you actually make your life easier since the system is easier to understand. >>> 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. I would actually rather have Row and Colum as specific items which can be asked for their header and iterate through all their values. Example: https://gist.github.com/rklemme/4771651 >>> 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. Right, and as I said I'd rather make that two separate steps. That is much more modular and hence reusable. >>> 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. See my initial statement: I think you are vastly underestimating the value of OOP. Cheers robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ -- [email protected] | https://groups.google.com/d/forum/ruby-talk-google?hl=en --- You received this message because you are subscribed to the Google Groups "ruby-talk-google" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/groups/opt_out.
