# A, b = Array.new(2).map!{|x| data.dup}

**URL:** <https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926>\
**Category:** ruby-talk\
**Created:** [23 February 2011 21:46 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926 "2011-02-23T21:46:15Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![Stefan\_Salewski](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Stefan\_Salewski](https://rubytalk.org/u/Stefan_Salewski)\
**Post date:** [23 February 2011 21:46 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/1 "2011-02-23T21:46:15Z")

</div>

I think I can replace this code

@scr = @output.dup  
@png = @output.dup  
@pdf = @output.dup  
@svg = @output.dup  
@ps = @output.dup

with this equivalent:

@scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}

Is there a nicer variant available?

(Of course @scr, @png, @pdf, @svg, @ps = @output.dup will not work, I  
need 5 independent instances.)

Thanks,

Stefan Salewski

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [23 February 2011 21:58 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/2 "2011-02-23T21:58:14Z")

</div>

Stefan Salewski wrote in post #983432:

> I think I can replace this code
> 
> @scr = @output.dup  
> @png = @output.dup  
> @pdf = @output.dup  
> @svg = @output.dup  
> @ps = @output.dup
> 
> with this equivalent:
> 
> @scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}
> 
> Is there a nicer variant available?

# here's one  
@scr, @png, @pdf, @svg, @ps = Array.new(5) { @output.dup }

# here's another, not for \<=1.8.6  
@scr, @png, @pdf, @svg, @ps = 5.times.map { @output.dup }

But I think the original 5 lines is clearer 🙂

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Peter\_Zotov](https://avatars.discourse-cdn.com/v4/letter/p/bbce88/32.png) [@Peter\_Zotov](https://rubytalk.org/u/Peter_Zotov)\
**Post date:** [23 February 2011 22:00 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/3 "2011-02-23T22:00:01Z")

</div>

@scr, @png, @pdf, @svg, @ps = 5.times.collect { @output.dup }

> **···**
>
> On Thu, 24 Feb 2011 06:46:15 +0900 Stefan Salewski \<mail@ssalewski.de\> wrote:
> 
> > I think I can replace this code
> > 
> > @scr = @output.dup  
> > @png = @output.dup  
> > @pdf = @output.dup  
> > @svg = @output.dup  
> > @ps = @output.dup
> > 
> > with this equivalent:
> > 
> > @scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}
> > 
> > Is there a nicer variant available?
> 
> --  
> &nbsp;&nbsp;WBR, Peter Zotov

---

<div class="post-metadata">

**Author:** ![niklas\_brueckenschla](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@niklas\_brueckenschla](https://rubytalk.org/u/niklas_brueckenschla)\
**Post date:** [23 February 2011 22:00 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/4 "2011-02-23T22:00:07Z")

</div>

Are you sure you can't rework your code to \*not\* copy data 5x? I assume  
you're doing conversion between formats, so the output will be something  
completely else anyway. No need to work in-place then (which I assume is  
the reason you're dup-ing).

-- niklas

> **···**
>
> On Thu, 2011-02-24 at 06:46 +0900, Stefan Salewski wrote:
> 
> > I think I can replace this code
> > 
> > @scr = @output.dup  
> > @png = @output.dup  
> > @pdf = @output.dup  
> > @svg = @output.dup  
> > @ps = @output.dup
> > 
> > with this equivalent:
> > 
> > @scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}
> > 
> > Is there a nicer variant available?
> > 
> > (Of course @scr, @png, @pdf, @svg, @ps = @output.dup will not work, I  
> > need 5 independent instances.)
> > 
> > Thanks,
> > 
> > Stefan Salewski

---

<div class="post-metadata">

**Author:** ![pp11](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pp11](https://rubytalk.org/u/pp11)\
**Post date:** [24 February 2011 08:42 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/5 "2011-02-24T08:42:11Z")

</div>

[@scr, @png, @pdf, @svg, @ps].collect!{|x| @output.dup}

> **···**
>
> > Date: Thu, 24 Feb 2011 06:46:15 +0900  
> > From: mail@ssalewski.de  
> > Subject: a, b = Array.new(2).map!{|x| data.dup}  
> > To: ruby-talk@ruby-lang.org
> > 
> > I think I can replace this code
> > 
> > @scr = @output.dup  
> > @png = @output.dup  
> > @pdf = @output.dup  
> > @svg = @output.dup  
> > @ps = @output.dup
> > 
> > with this equivalent:
> > 
> > @scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}
> > 
> > Is there a nicer variant available?
> > 
> > (Of course @scr, @png, @pdf, @svg, @ps = @output.dup will not work, I  
> > need 5 independent instances.)
> > 
> > Thanks,
> > 
> > Stefan Salewski

---

<div class="post-metadata">

**Author:** ![king\_key](https://avatars.discourse-cdn.com/v4/letter/k/e480ec/32.png) [@king\_key](https://rubytalk.org/u/king_key)\
**Post date:** [27 February 2011 16:00 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/6 "2011-02-27T16:00:14Z")

</div>

[www.wholesalesnkey.net](http://www.wholesalesnkey.net)  
windows 7  
office 2007  
office 2010  
office visio key  
windows visio key  
windows xp key

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [23 February 2011 22:05 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/7 "2011-02-23T22:05:41Z")

</div>

\*ding \*\*ding\* \*ding\* \*ding\* \*ding\*

WE HAVE A WINNAR!

> **···**
>
> On Feb 23, 2011, at 14:00 , niklas | brueckenschlaeger wrote:
> 
> > Are you sure you can't rework your code to \*not\* copy data 5x? I assume  
> > you're doing conversion between formats, so the output will be something  
> > completely else anyway. No need to work in-place then (which I assume is  
> > the reason you're dup-ing).

---

<div class="post-metadata">

**Author:** ![Stefan\_Salewski](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Stefan\_Salewski](https://rubytalk.org/u/Stefan_Salewski)\
**Post date:** [23 February 2011 22:11 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/8 "2011-02-23T22:11:41Z")

</div>

Stefan Salewski wrote in post #983432:

> \> Is there a nicer variant available?
> 
> # here's one  
> @scr, @png, @pdf, @svg, @ps = Array.new(5) { @output.dup }
> 
> # here's another, not for \<=1.8.6  
> @scr, @png, @pdf, @svg, @ps = 5.times.map { @output.dup }
> 
> But I think the original 5 lines is clearer 🙂

Thanks. I agree, but I am still learning Ruby, so I like to see what is  
possible. And I like to save some lines of code. On the other hand,  
plain code is easier to understand and can easier converted to other  
languages, I know.

> **···**
>
> On Thu, 2011-02-24 at 06:58 +0900, Brian Candler wrote:

---

<div class="post-metadata">

**Author:** ![Stefan\_Salewski](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Stefan\_Salewski](https://rubytalk.org/u/Stefan_Salewski)\
**Post date:** [23 February 2011 22:24 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/9 "2011-02-23T22:24:14Z")

</div>

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. I can avoid duplicates, of course, if I look at the  
basic hash and at the special variants each time. But that is a little  
bit more complicated and consumes more time, which is not really good.

All that is related to a graphical editor -- I have to clean up the code  
a bit, it will become available on my page some time, then we can  
discuss improvements. A tiny part related to cairo drawing is already  
there:

[http://www.ssalewski.de/PetEd-Demo.html.en](http://www.ssalewski.de/PetEd-Demo.html.en)

> **···**
>
> On Thu, 2011-02-24 at 07:00 +0900, niklas | brueckenschlaeger wrote:
> 
> > Are you sure you can't rework your code to \*not\* copy data 5x?

---

<div class="post-metadata">

**Author:** ![pp11](https://avatars.discourse-cdn.com/v4/letter/p/e9c0ed/32.png) [@pp11](https://rubytalk.org/u/pp11)\
**Post date:** [24 February 2011 08:44 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/10 "2011-02-24T08:44:52Z")

</div>

oops, this doesn't work.... sorry

> **···**
>
> > Date: Thu, 24 Feb 2011 17:42:11 +0900  
> > From: tdihp@hotmail.com  
> > Subject: Re: a, b = Array.new(2).map!{|x| data.dup}  
> > To: ruby-talk@ruby-lang.org
> > 
> > [@scr, @png, @pdf, @svg, @ps].collect!{|x| @output.dup}
> > 
> > \> Date: Thu, 24 Feb 2011 06:46:15 +0900  
> > \> From: mail@ssalewski.de  
> > \> Subject: a, b = Array.new(2).map!{|x| data.dup}  
> > \> To: ruby-talk@ruby-lang.org  
> > \>  
> > \> I think I can replace this code  
> > \>  
> > \> @scr = @output.dup  
> > \> @png = @output.dup  
> > \> @pdf = @output.dup  
> > \> @svg = @output.dup  
> > \> @ps = @output.dup  
> > \>  
> > \> with this equivalent:  
> > \>  
> > \> @scr, @png, @pdf, @svg, @ps = Array.new(5).map!{|x| @output.dup}  
> > \>  
> > \> Is there a nicer variant available?  
> > \>  
> > \> (Of course @scr, @png, @pdf, @svg, @ps = @output.dup will not work, I  
> > \> need 5 independent instances.)  
> > \>  
> > \> Thanks,  
> > \>  
> > \> Stefan Salewski  
> > \>  
> > \>  
> > \>

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [24 February 2011 03:49 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/11 "2011-02-24T03:49:35Z")

</div>

ok, try also something like,

&nbsp;&nbsp;a,b,c,d,e =[x.dup]\*5

best regards -botp

> **···**
>
> On Thu, Feb 24, 2011 at 6:24 AM, Stefan Salewski \<mail@ssalewski.de\> wrote:
> 
> > On Thu, 2011-02-24 at 07:00 +0900, niklas | brueckenschlaeger wrote:
> > 
> > > Are you sure you can't rework your code to \*not\* copy data 5x?
> > 
> > The code is related to handling configuration files, I have a basic

---

<div class="post-metadata">

**Author:** ![Brian\_Candler](https://avatars.discourse-cdn.com/v4/letter/b/5f9b8f/32.png) [@Brian\_Candler](https://rubytalk.org/u/Brian_Candler)\
**Post date:** [24 February 2011 09:09 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/12 "2011-02-24T09:09:13Z")

</div>

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.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Anurag\_Priyam](https://avatars.discourse-cdn.com/v4/letter/a/df705f/32.png) [@Anurag\_Priyam](https://rubytalk.org/u/Anurag_Priyam)\
**Post date:** [24 February 2011 05:40 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/13 "2011-02-24T05:40:33Z")

</div>

> 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.

x = "foo"  
a, b, c, d, e = [x.dup] \* 5  
puts a #=\> "foo"  
b \<\< " bar"  
puts a #=\> "foo bar"

> **···**
>
> --  
> Anurag Priyam  
> [http://about.me/yeban/](http://about.me/yeban/)

---

<div class="post-metadata">

**Author:** ![niklas\_brueckenschla](https://avatars.discourse-cdn.com/v4/letter/n/41988e/32.png) [@niklas\_brueckenschla](https://rubytalk.org/u/niklas_brueckenschla)\
**Post date:** [24 February 2011 12:23 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/14 "2011-02-24T12:23:52Z")

</div>

> 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 }

This would be the same as:

&nbsp;&nbsp;@configs = Hash.new {|h,k| h[k] = @output.dup }

after that you can use pretty much any key you want 🙂

-- niklas

> **···**
>
> On Thu, 2011-02-24 at 18:09 +0900, Brian Candler wrote:
> 
> > 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.

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [24 February 2011 06:11 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/15 "2011-02-24T06:11:36Z")

</div>

> > 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,

indeed. but they will not point to or clobber x

> which might result in an unexpected side effect.

indeed. but it may have a good effect too. the user is working on graphics.

let's just see if user has a need for it...

best regards -botp

> **···**
>
> On Thu, Feb 24, 2011 at 1:40 PM, Anurag Priyam \<anurag08priyam@gmail.com\> wrote:

---

<div class="post-metadata">

**Author:** ![Stefan\_Salewski](https://avatars.discourse-cdn.com/v4/letter/s/ecae2f/32.png) [@Stefan\_Salewski](https://rubytalk.org/u/Stefan_Salewski)\
**Post date:** [24 February 2011 12:53 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/16 "2011-02-24T12:53:03Z")

</div>

> \> 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

> **···**
>
> On Thu, 2011-02-24 at 11:10 +0530, Anurag Priyam wrote:

---

<div class="post-metadata">

**Author:** ![Josh\_Cheek](https://avatars.discourse-cdn.com/v4/letter/j/e79b87/32.png) [@Josh\_Cheek](https://rubytalk.org/u/Josh_Cheek)\
**Post date:** [24 February 2011 07:44 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/17 "2011-02-24T07:44:01Z")

</div>

Well, then you could just do  
a = b = c = d = e = x.dup

No need for parallel assignment tricks if you are okay with them pointing to  
the same object.

> **···**
>
> On Thu, Feb 24, 2011 at 12:11 AM, botp \<botpena@gmail.com\> wrote:
> 
> > On Thu, Feb 24, 2011 at 1:40 PM, Anurag Priyam \<anurag08priyam@gmail.com\> \> 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,
> > 
> > indeed. but they will not point to or clobber x

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [24 February 2011 08:28 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/18 "2011-02-24T08:28:56Z")

</div>

my bad. should have read the whole op post... "..@stefan:.. I  
need 5 independent instances..."

sorry for the noise.  
kind regards -botp

> **···**
>
> On Thu, Feb 24, 2011 at 2:11 PM, botp \<botpena@gmail.com\> wrote:
> 
> > let's just see if user has a need for it...

---

<div class="post-metadata">

**Author:** ![botp1](https://avatars.discourse-cdn.com/v4/letter/b/6de8d8/32.png) [@botp1](https://rubytalk.org/u/botp1)\
**Post date:** [24 February 2011 08:31 UTC](https://rubytalk.org/t/a-b-array-new-2-map-x-data-dup/61926/19 "2011-02-24T08:31:45Z")

</div>

indeed. i was too tight on parallels and arrays and forgetting the  
fundamentals =)  
thanks and kind regards -botp

> **···**
>
> On Thu, Feb 24, 2011 at 3:44 PM, Josh Cheek \<josh.cheek@gmail.com\> wrote:
> 
> > Well, then you could just do  
> > a = b = c = d = e = x.dup
