From: Pit Capitain Date: 2002-07-10T19:16:24+09:00 Subject: Re: Case expressions and LHS of assignment.... On 10 Jul 2002, at 19:01, Hugh Sasse Staff Elec Eng wrote: > On Wed, 10 Jul 2002, Peter Hickman wrote: > > > Perhaps you could use an array to hold you instance variables rather > > than the 3 seperate ones. This then gives > > > > def initialize > > @values = Array.new > > (1..3).times { @value << "Initial" } > > end > > > > def assign(x,y) > > @values[x] = y > > end > > > > Even shorter (especially without error checking :-) ) and much much > > clearer. > > You're taking the contrived, cut-down example too literally :-). The > numbers were easier to type to test this out, I was using symbols in > fact, :x, :y, and :z in the first place, but the problem is more > general than that because a case statement can be used with anything > that copes with ===. You can still use a similar approach using a hash with symbols as keys. In the assignment, use the case expression to detemine the key. Something like def initialize @values = Hash.new( "Initial" ) end def assign( x, y ) @values[ case x when ... :x when ... :y else :z end ] = y end Regards, Pit