On Tue, Apr 7, 2009 at 2:50 PM, Ruby Student <ruby.student / gmail.com> wrote:
> Hello all you happy people!
>
> I have been thinking about asking this question but did not know how to
> start.
> So, if my question is ambiguous please let me know and I will try to make it
> clearer.
>
> Given an NxN array or even an NxM array mA from which at times I break it
> into sub arrays, I would like to be able to manipulate the sub arrays and
> have the changes dynamically propagated to the main array mA.
> For example, assume I have main array mA as listed below.
> From mA I strip say row[2], which I named sAr2 and which has values: sAr2 =
> [D, C, B, A]
> I also created sub arrays of columns, as listed below and furthermore, I
> created quadrants sub arrays.
>
> I would like to make a change on any of the sub arrays and have that change
> propagated simultaneously to the main array.
> For instance, say I set sAr2[1] = Y, I would like element mA[2,1] to change
> to Y from C.

First thing that came to mind is to add a level of indirection, so
that the array doesn't
contain the actual values, but an object that contains the values, like so:

irb(main):001:0> O = Struct.new :value
=> O
irb(main):008:0> A = "A"
=> "A"
irb(main):009:0> B = "B"
=> "B"
irb(main):010:0> C = "C"
=> "C"
irb(main):013:0> myAr = [[O.new(A), O.new(B), O.new(C)],[O.new(B),
O.new(A), O.new(C)],[O.new(C), O.new(B), O.new(A)]]
=> [[#<struct O value="A">, #<struct O value="B">, #<struct O
value="C">], [#<struct O value="B">, #<struct O value="A">, #<struct O
value="C">], [#<struct O value="C">, #<struct O value="B">, #<struct O
value="A">]]
irb(main):017:0> first_row = myAr[0]
=> [#<struct O value="A">, #<struct O value="B">, #<struct O value="C">]
irb(main):018:0> Y = "Y"
=> "Y"
irb(main):019:0> first_row[2].value = Y
=> "Y"
irb(main):020:0> myAr
=> [[#<struct O value="A">, #<struct O value="B">, #<struct O
value="Y">], [#<struct O value="B">, #<struct O value="A">, #<struct O
value="C">], [#<struct O value="C">, #<struct O value="B">, #<struct O
value="A">]]

So by changing the value of the object pointed to by first_row[0], you
are really changing the same object that is referenced in the original
array. Don't know if this is convenient for you or not. Probably
someone will come up with a better idea.

Hope this helps,

Jesus.