matthew.moss.coder
3/24/2006 4:52:00 AM
> How do I build a function that, when called by the Calc button callback,
> fetches the contents of the 9 numbers and returns it as a 3x3 matrix?
> From what I've read, ruby doesn't have real two dimensional arrays. So
> am I stuck doing everything in a manner that doesn't map well to my math
> problem domain?
You can do 2d arrays in Ruby as an array of arrays. (Very much like
many other languages.)
There's also the enumerator module that can help build a 3x3 array out
of a 9-element sequence. In irb:
> require 'enumerator'
=> true
> a = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]
> b = []
=> []
> a.each_slice(3) { |x| b << x }
=> nil
> b
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
> b[1][2]
=> 6
You can use Matrix to store things other than Float, but most of the
methods on Matrix only work with and are intended for Float. So
putting things other than Float into Matrix turns it into an array of
arrays; might as well skip Matrix.