Array simple question

Ok - my search again tells me there is no "copy" method in Ruby (is that
right now??) , so an answer 'just dump' would have been nice...?!

Berg

Or: doing some research first, and *then* asking which of the available
methods would be best, or whether there exist other approaches you
might have overlooked...

And it's always good to give some context and explain what you want
to achieve. Maybe there would have been a better choice than using
an array, or whatever.

Regards,
Marcus

···

Am 06.03.2016 um 09:43 schrieb A Berger:

Ok - my search again tells me there is no "copy" method in Ruby (is that
right now??) , so an answer 'just dump' would have been nice...?!

Hello
There are always alternatives!

I think if I write about all the things around my question, than it takes
much more time to read this.
just a simple question "is there a .dup for full copy of moredimemsional
arrays?" allowing a precise answer is much easier to answer. Who is
interested in the rest?

Thx Berg

We are. Because most of the time there isn't "the" answer,
the best solution usually depends on the specific use case.

···

Am 06.03.2016 um 16:03 schrieb A Berger:

Hello
There are always alternatives!

I think if I write about all the things around my question, than it
takes much more time to read this.
just a simple question "is there a .dup for full copy of
moredimemsional arrays?" allowing a precise answer is much easier to
answer. Who is interested in the rest?

But it takes even more of our time to provide you with 5 different
possible solutions, instead of simply giving the one that's most suited
to your problem.

···

Am 06.03.2016 um 16:03 schrieb A Berger:

I think if I write about all the things around my question, than it
takes much more time to read this.

In this case the aim was only to full-copy an array.
It looks like there are is no
array.fulldup (-equal) method.

Which other (efficient) ways do exist?

I'm leaning Ruby, so I'm interested in other solutions for a given (Ruby)
problem, too!

To make Ruby better
also a method "ArrayString to Array"
Array.new(...).to_s.to_a
would be nice.

Berg

You need to understand that the approach taken depends on the context,
more specifically on the content of the Array and the usage of
original and copy. There is no solution for "copying Array" that will
always work or is always optimal.

Regards

robert

···

On Sun, Mar 6, 2016 at 7:04 PM, A Berger <aberger7890@gmail.com> wrote:

In this case the aim was only to full-copy an array.

--
[guy, jim, charlie].each {|him| remember.him do |as, often| as.you_can
- without end}
http://blog.rubybestpractices.com/

In this case the aim was only to full-copy an array.
It looks like there are is no
array.fulldup (-equal) method.

Which other (efficient) ways do exist?

I'm leaning Ruby, so I'm interested in other solutions for a given (Ruby)

problem, too!

Usually the actual solution is: don't do it; refactor your code so you
don't need a deep copy, or clearly document the issue in your API.

This is why it can be helpful to explain your situation in more detail; we
might be able to point out where to backtrack, or rethink your solution.

To make Ruby better
also a method "ArrayString to Array"
Array.new(...).to_s.to_a
would be nice.

That assumes all objects (including Array) have a #to_s that contains
enough information to completely rebuild the object. Which they don't, by
design. #to_s is like casting an object to a String, some fidelity is
expected to be lost.

And as for String#to_a, if I discovered that method's existence, in
isolation from the current issue, I'd assume it's a short-hand for
.each.to_a (which used to be a thing back in 1.8, giving an array of
Strings representing the individual lines in the original string.) I
certainly wouldn't assume it parses/evals the String.

Also, if I had this:

    str = "[1, :b, \"foo\", [7]]"

...and it had a .to_a method that could recognise brackets and commas and
all that, what would it call (recursively, inside #to_a) on the various
substrings?

I.e. what can you call on "\"foo\"" that gives "foo" ?

You're looking for a serialisation, and yes, those exist.

···

On 07/03/2016 4:05 AM, "A Berger" <aberger7890@gmail.com> wrote:

Hello
Nice answer!
Perhaps I'll never use deep-copying arrays. This problem arised, because i
saw that .dup didnt copy all.

With serialization you mean dump?
Seems that's the best way (but its output is ugly). (Recursive) pattern
matching+splitting to make an Array out of array.to_s would be nicer.
(a.to_s is much more readable than the dump-output)
I assume its a 1liner with pushes, but not easy to be done.
I didnt get a solution, two days for that should be enough.

Berg

Hello
Nice answer!
Perhaps I'll never use deep-copying arrays. This problem arised, because i
saw that .dup didnt copy all.

With serialization you mean dump?

Serialisation is a universal concept, independent of the language/libraries
involved. The standard Ruby implementation uses the Marshal module[1],
which, yes, defines a #dump method. Depending on the data involved, though,
you could use all sorts of possible serialisation formats (json, YAML, etc.)

Seems that's the best way (but its output is ugly).

You were asking about deep copy, not about inspecting intermediate data
serialisations. It's not generally intended for human consumption​.
Although, as I say, json and YAML (etc.) can also work, assuming you have
appropriate data structures; and they are much prettier.

(Recursive) pattern matching+splitting to make an Array out of array.to_s
would be nicer. (a.to_s is much more readable than the dump-output)
I assume its a 1liner with pushes, but not easy to be done.
I didnt get a solution, two days for that should be enough.

Sorry I can't quite understand what you're saying in this paragraph. If you
think it's simple, how would you round-trip this Array?

    [Object.new, Object.new.tap{|o| def o.foo() 1; end }]

Sure, if all the members of the Array are primitive singletons (like
Fixnums and Symbols) it's pretty easy; but as soon as you get beyond that
you have to start delving into reflection (to work out how to
programatically reconstruct identical-but-different objects) and what the
definition of "identical" even is (answer this: are two arrays identical if
the addresses of the objects inside them are different?) And even then, if
you rely on something well-defined like #== or #equal? to define your
version of "identical", how can you construct an object that satisfies it?
(How can you know that `a == b` or `c.equal? d` before evaluating the test?)

If you describe the details of the situation you're in, it might be easier
to provide a solution. For example, if your Array only ever contains
Fixnums then the answer we give will be very different from if you were
talking about arbitrary objects.

Cheers

[1]: http://ruby-doc.org/core-2.3.0/Marshal.html​

···

On 7 March 2016 at 10:08, A Berger <aberger7890@gmail.com> wrote:
--
  Matthew Kerwin
  http://matthew.kerwin.net.au/