From: Stefan Salewski Date: 2011-02-24T21:53:03+09:00 Subject: Re: a, b = Array.new(2).map!{|x| data.dup} On Thu, 2011-02-24 at 11:10 +0530, Anurag Priyam wrote: > > ok, try also something like, > > > > a,b,c,d,e =[x.dup]*5 > > That will cause a, b, c, d, e to point to the same objects, which > might result in an unexpected side effect. > Indeed, that was not what I wanted, I need 5 independent instances! Brian Candler wrote: >Stefan Salewski wrote in post #983443: >> The code is related to handling configuration files, I have a basic >> configuration set, a hash called @output. And variants for on screen, >> pdf, png export. > >In that case, I'd say you'd be better off avoiding all the separate >instance variables for each variant, and make it data-driven. Then you >can interate over the variants, which will naturally avoid the repeated >code. > >FORMATS = [:scr, :png, :pdf, :svg, :ps] >... >@configs = {} >FORMATS.each { |k| @configs[k] = @output.dup } > >Now you have @configs[:scr], @configs[:png] etc, all pointing to >separate copies of @output. And if you want to add another format in >future, it's a trivial change. > >(Aside: please do not post variants of this code using 'inject', because >it's stupid here. You know who you are. Thank you.) > >Regards, > >Brian. I see the point, thanks. Best regards, Stefan Salewski