Wrong cloning of Sketchup::Color objects
-
For information, there is an error in the way colors created as
Sketchup::Colorobjects answer to theclonemethod.Easy to reproduce in the Ruby console (all versions of SU, including SU2016)
c = Sketchup;;Color.new 'blue' Color( 0, 0, 255, 255) c.clone Color( 0, 0, 0, 0)This is annoying if you do not know the problem and duplicate hash arrays by cloning.
The workaround is simply
cloned_color = Sketchup::Color.new(original_color).But, I see no metaphysical reason why
clonewould not work correctly. It works fine on Point3d, Vector3d and Transformations. So I logged a bug to the Sketchup team.Fredo
-
hm... I think I ran into this myself. Might be an omission.
Btw, in regard to point3d and vector3d - one would normally use dup - but for some reason only clone was implemented for these classes and I think it acts like dup should have been.
-
I use
c = (Sketchup;;Color.new 'blue').to_a # => [0, 0, 255, 255] c.dup # => [0, 0, 255, 255]can't remember why, but maybe for the same reason...
john
-
c2 = Sketchup::Color.new(c1.to_a) -
The workaround is easy to find, but that was the bug which was more difficult to identify.
I still think it is a bug. Either
Sketchup::Colordoes not supportclone, or it does support correctly!Fredo
-
True that.
-
Dan,
We know that most Sketchup objects won't behave correctly with
cloneordup.However, and wrongly, I thought Sketchup::Color was just an encapsulation of a 4-value array and would behave as Geom::Point3d or Geom::Transformation.
Now I know it's not the case and I can live with it, because I know.
Fredo
-
@thomthom said:
hm... I think I ran into this myself. Might be an omission.
Yes, you did. You posted a public announcement in 2010:
http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=32591@fredo6 said:
This is annoying if you do not know the problem and duplicate hash arrays by cloning.
The workaround is simply
cloned_color = Sketchup::Color.new(original_color).Which goes against the API documentation be inadequate (again.)
@fredo6 said:
It works fine on
Point3d,Vector3dandTransformations.Because the API explicitly overrides the
clone()method inherited from object, in those three API classes.
http://ruby-doc.org/core-2.0.0/Object.html#method-i-clone@fredo6 said:
But, I see no metaphysical reason why
clonewould not work correctly.The Ruby interpreter follows precise "rules" of it's Object Oriented Programming model, .. so metaphysics (or abstract) reasons do not apply.

In the case of Ruby's
cloneanddup@unknownuser said:
Ruby Doc: Object#clone()[/url:3ety1hw5]":3ety1hw5]This method may have class-specific behavior. If so, that behavior will be documented under the
#initialize_copymethod of the class.So, a custom class needs to implement an
#initialize_copy()method, in order to leverage Ruby's built-in cloning / dup'ing (so Ruby knows how to create the object copy,) or locally override the methods directly.For examples of how arrays, hashes and strings are cloned see:
- [url=http://ruby-doc.org/core-2.0.0/Hash.html#method-i-initialize_copy:3ety1hw5]#initialize_copy (Hash)[/url:3ety1hw5] [you must click to see the C source]
Just making an assumption, that is outside the "order of Ruby things" is bound to lead to failure or frustration.
So the historical correct assumption has been, "If API classes (especially those that are model entity classes,) have not published a specific cloning method overrides,.. then don't use
clone()ordup()with them."How do you know when API classes implement
clone()?Refer to the API method index for "c":
http://www.sketchup.com/intl/en/developer/docs/methods#index_c@fredo6 said:
So I logged a bug to the Sketchup team.
Which I also did like 7/8 years ago. So, it is already logged.
Other old threads on this:
2010: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=29138%26amp;p=253940#p253787
2011: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=40201%26amp;p=355527#p355527
2014: http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=58083%26amp;hilit=clone%26amp;p=530348#p530015
Related... concerning lack of
Geom::PolygonMeshclone() & dup() methods:
http://sketchucation.com/forums/viewtopic.php?f=180%26amp;t=58080%26amp;p=530014#p530014 -
@fredo6 said:
However, and wrongly, I thought
Sketchup::Colorwas just an encapsulation of a 4-value array ...But that does bring up a very good point, that because it is compatible with an array (ie, has a
to_amethod and it's constructor accepts an array,)... making aclonemethod is beyond simple:For Ruby 2.0+;
module Fredo6;;Cloner refine ;;Sketchup;;Color do def clone() self.class;;new(self.to_a) end end end using Fredo6;;Cloner module Fredo6;;SomePlugin # code that uses refined Color class end
Advertisement