On Thu, Feb 14, 2013 at 11:50 PM, Joel Pearson <[email protected]> wrote: > Interesting Matrix build. It's giving me a bit of a headache just trying > to figure out the links involved. > > So MatrixPart defines the methods and the "parent" matrix (held as an > instance variable); and row and column both use these methods and both > access the variable which points to the matrix they're part of.
Yes, Row and Column are a facade to the "real" data and provide a different interface to it which presents a different abstraction: while the Matrix has two dimensions a Row and a Column only have one. > The rows and columns can be selected based on given headers, and each > will reference the other... and this is where my head explodes: > > def index( row, col ) > @row_headers.index( row ) * @col_headers.size + @col_headers.index( > col ) > end > > It takes a bit of getting used to, but thanks to Ruby's flexible array > class adding nil values automatically when you specify an index higher > than the upper boundary, that works. Since you obviously understood the method now I am not sure why you say your head explodes over this piece of code. Btw, with a small change you can change storage of data from an Array to a Hash making the Matrix class better suited for sparse matrices. And here comes an important aspect of that implementation: only the Matrix class had to change, there was absolutely no change necessary for the other three classes! This shows how Matrix's API isolated client code from inner workings of this class. This is what OO is about. https://gist.github.com/rklemme/4771651/revisions > I guess with a bit more poking and prodding I could figure out how to > append, insert, and delete rows and columns. After all, it's only a math > problem in the end. All the interconnected references (especially the > layered yields) still make my head spin though :) You'll get used to that - and with a bit of oil the squeaking goes away as well. ;-) Kind regards 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.
