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

S

Stefan Salewski

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
 
B

Brian Candler

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 :)
 
P

Peter Zotov

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?

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

Ryan Davis

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

*ding **ding* *ding* *ding* *ding*

WE HAVE A WINNAR!
 
S

Stefan Salewski

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

Stefan Salewski

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

Anurag Priyam

ok, try also something like,
=A0a,b,c,d,e =3D[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 =3D "foo"
a, b, c, d, e =3D [x.dup] * 5
puts a #=3D> "foo"
b << " bar"
puts a #=3D> "foo bar"

--=20
Anurag Priyam
http://about.me/yeban/
 
B

botp

ok, try also something like,

=A0a,b,c,d,e =3D[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
 
J

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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

botp

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

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

sorry for the noise.
kind regards -botp
 
B

botp

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

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

pp

[Note: parts of this message were removed to make it a legal post.]


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

pp

[Note: parts of this message were removed to make it a legal post.]


oops, this doesn't work.... sorry
Date: Thu, 24 Feb 2011 17:42:11 +0900
From: (e-mail address removed)
Subject: Re: a, b = Array.new(2).map!{|x| data.dup}
To: (e-mail address removed)


[@scr, @png, @pdf, @svg, @ps].collect!{|x| @output.dup}
Date: Thu, 24 Feb 2011 06:46:15 +0900
From: (e-mail address removed)
Subject: a, b = Array.new(2).map!{|x| data.dup}
To: (e-mail address removed)

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
 
B

Brian Candler

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

Stefan Salewski

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 said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,139
Messages
2,570,805
Members
47,352
Latest member
DianeKulik

Latest Threads

Top