So even though I am operating on b which is a copy of the Array a, the elements of a are still modified. If you are not familiar with why, then this is worth spending a few minutes to figure out; or figuring out again, in my case.

TIG wrote: because the variablesa and b will then both refer to the same array?

The references (not variables!) do not refer to the same array after the b = a.dup call. (Check their object ids.)They DO however point at the SAME objects in YOUR example because Integers are Immediate objects (there is ONLY 1 of each ordinal in the set of Ruby Integers.)After you ref a to point at the object that b points at, then both refs point at the same Array object.

In Jim's example, he uses Strings, which are NOT Immediate objects. You can have more than 1 String "one", but in his case, the .dup method copies the Array object, but does not duplicate the individual element objects.

It seems "nutty" but this is one of the lessons rubyists should be taught early. In Ruby, everything is an object, every object has a reference. Literal arguments, are converted to objects and assigned an anonymous reference by RUBY. Since the element objects in the array have a 'parent' object that IS referenced, they will not be swept up by Ruby's GC, even though they don't have a explicit reference.

So.. an array is not a set of values (like it may be in BASIC,) in Ruby an array is a set of references, either explicit (managed by YOU,) or anonymous (managed by Ruby.)When you dup or clone the array, you copy the references.

If you wish a completely different array:b = [] # a new Array objecta.each {|e| b << e.dup}

TIP In Ruby = is NOT "equals" (which is why there is an .equal? method.)It is the assignment operator, so your better off thinking and saying 'SPA' (Shall Point At.) Read:a = bas "The reference ashall point at the object that b references."

TIG wrote:Perhaps your a = %w( one two three)is not quite the same as a = ["one", "two", "three"]?

So when you .dup an array, you get a new array. Because the elements of the original array are references to a non-primitive type (String in this case,) the new array's elements reference the original elements.

As Dan says, there are no variables in Ruby, only references to object.

When you copy an object, all you do is copy its set of instance variables, which are just references to other objects. For an array, the instance variables are its set of indexes, which again are just references. Copying an array just means making a new list of references, but the objects they point to remain unmodified and uncopied.

FWIW This whole area of Comp Sci is pretty tricky to resolve in programming languages generally because ultimately these references are not 'semantic free'.Sometimes you want a 'deep' copy and sometimes you want a 'shallow' copy. The classic example is if you have a class Car which has an attribute which is a reference to the car manufacturer, when I copy the car, I generally wouldn't want to deep copy the entire Ford motor company (or whatever).

So languages essentially cannot a priori "know" what the meaning and therefore the intent of these references are, hence we have to either manually descend a parts hierarchy choosing to copy or not copy, or keep a top-level reference.

Programming Ruby in Object#dup wrote:In general, clone and dup may have different semantics in descendent classes. While clone is used to duplicate an object, including its internal state, dup typically uses the class of the descendent object to create the new instance.

In general it is always safer to try using dup first (especially if you don't want to get a frozen object.)

thomthom wrote:I use clone a lot for SketchUp's objects... What is the issue with SU's .clone?

It WILL work for any classes that are subclasses of standard Ruby classes, such as Length, which is a subclass of Float.

It WILL work for any custom Sketchup classes that provide an overidden version of clone, such as:Geom::Point3dGeom::TransformationGeom::Vector3d* note that these work like dup and not clone (in that they do not copy the frozen state of the receiver.)

The Ruby inherited edition will not work for many C++ objects like Sketchup::Face.

To get around this, TIG shows in the other thread, how to use Sketchup::Group.copy to "clone" Sketchup::Drawingelement subclass objects, like Faces, Edges, etc.

Geom::BoundingBox objects can be copied by using their .add method (if clone or dup does not work):bb2 = Geom::BoundingBox.new.add( bb1 )

thomthom wrote:So SU doesn't override .dup for Point3d and Vector3d?That would explain why I had problems before when I tried .dup for Point3d. I've had to use .clone.

Yepper... I remember having that discussion with you in another topic thread.

Either most the Sketchup classes need to override Ruby's .dup and .clone with methods that work, or they should be removed for those classes.

And... a point about .freeze, we would not want to freeze most of the Ruby objects that Sketchup needs to modify (data classes and any object class that is kept in the model.) So copying the frozen state doesn't mean much, so the overriden dup could likely be just an alias for the overriden clone.

In most Sketchup classes the .freeze method should be also removed. It can be done for many of them, by removing it from Sketchup::Entity.

I passing... IF you want to make a completely separate array based on another array's 'reference' then us[]+soa=[1,2]b=[]+amakes array a and array b separate arrays as a[0]=99givesa ==> [99,2]but b is not affectedb ==> [1,2]?

I am SO glad that I found this 5-year-old thread. I, too, was having trouble copying an array and having the copy be independent of the original.

In my case, I had an array of points that I had generated, and I wanted to create a closed curve from them. My strategy was to copy the point array curve = [] curve = pointand then add point[0] to the end, curve << point[0]so that a curve created from the new curve[] array would return to its beginning point. path = ents.add_curve curve

But the above sequence kept adding the extra point to the point array too.

The assignmentcurve = pointmeans the array 'curve' refers to the array 'point' - they are essentially referencing the same thing, whereas...curve = point.clonemeans the array 'curve' is a separate array, which has been made as a copy from the array 'point',Consider this...curve = point + [ point[0] ]which achieves you aim for a 'curve' array defining a 'loop', but done in the one step.It combines the array 'point' and a new array made from the first element of that array, all in a new array named 'curve'.

Incidentally consider naming arrays and other collections in the plural - it is is easy to follow the code - so the array named 'points' consists of a collection of elements, each of which is a 'point'.The 'curve' array would also perhaps be better named 'curve_points'

I had not tried using that additional set of square brackets. It makes sense.

As for naming, I thought about using plural in the first place, but most of my usages were as singular references, point[0], point[1], ... point[n-1], point[n] where the singular read better to me. I like "curve_points" -- that is always used as a group, never individually, so plural reads much better there.

Thanks,August

0

“An idea, like a ghost, must be spoken to a little before it will explain itself.”

This page, <http://lukaszwrobel.pl/blog/copy-object-in-ruby> suggests that a clone's elements still point to the original elements so some changes to one will indeed show up in the other. That's what I found with my initial testing.

Above, Dan says

It WILL work for any custom Sketchup classes that provide an overidden version of clone, such as:Geom::Point3dGeom::TransformationGeom::Vector3d* note that these work like dup and not clone (in that they do not copy the frozen state of the receiver.)

The Ruby inherited edition will not work for many C++ objects like Sketchup::Face.

which shows that for these two kinds of changes, the clone is not affected.

I'm still not clear on when I can use .clone and when not, nor do I really understand shallow vs. deep copies and frozen objects, so for now, unless I'm doing tens of thousands of copies, I may use a brute force method becuase the shortcuts seem so problematical.

Thanks,=A=

0

“An idea, like a ghost, must be spoken to a little before it will explain itself.”

Any call to using() must occur within the TOPLEVEL_BINDING.This restriction has been removed in later versions of Ruby 2.2+, and the experimental warning that is output on calls to refine has also been removed. (Ie, refinements are no longer experimental and calls to using can happen inside specific module and class scopes.)

If you want to see that all the elements of the arrays are different objects, you can iterate them and compare their object id numbers.

Back to TIG's comment about plural vs. singular names for arrays, here is the way I'm naming things. In this context, the singular terms seem to work better for me, probably because I think of it as the mathematical notation P0, P1, ... PN.