Problem with group.copy
-
This could have been my problem back in June as well.
Can you think of any way to lock a method stoat other rubies cannot override it. (This is mch worse than adding a new method to an existing class.) then SU could just lock aloof its methods.
-
The author of SketchyPhysics is aware of the issues and has said these will be addressed in the next update. It doesn't even use the changed group.copy method anyway
In the meantime rather than using
gcopy = group.copy
Which should return a reference to the new instance of the group, but might return 'nil' if SP messes with it... in your code consider something like this instead to side step it:
gcopy = group.parent.entities.add_instance(group.entities.parent, group.transformation)
This return the instance, placing the copy immediately over the original group, in the same context.Incidentally, you can also use this method to 'duplicate' a group inside another context, like:
gcopy = someother_entities.add_instance(group.entities.parent, group.transformation)
[Note: you might need to adjust 'transformation' depending on the contexts involved]
To 'move the group' addgroup.erase!; group = gcopy
- this will then appear that the group has moved into the new entities [like Edit>Cut...Edit>PasteInPlace].
The second step re-references 'group' to point to the new copy, as its 'id' etc will have changed too, this way it keeps an enduring reference, useful if you are to do something with it later on. -
@scottbattersby said:
I have key-shaped bruises on my forehead after spending a morning trying to work out why group.copy was returning nil when applied to a perfectly normal group.
TIG writes : "Perhaps he has a third party script that ill-advisedly refines the group.copy method and makes it return nil ???"
So I take out all scripts from my plugins directory and Hey Presto! suddenly group.copy works as advertised.
I was reading this thread and was just about to make a comment about group.copy being modified by some plugin - though I could not remember which one. Glad you pinned it down.
As TIG said, SketchyPhysics is being reworked to avoid such clashes.
-
@render plus said:
Can you think of any way to lock a method stoat other rubies cannot override it. (This is mch worse than adding a new method to an existing class.) then SU could just lock aloof its methods.
<span class="syntaxdefault"><br />module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Hello world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => nil<br /><br /></span><span class="syntaxdefault">FooBar</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment"># => FooBar<br /><br /></span><span class="syntaxdefault">module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">bye</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Goodbye world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => Error; #<TypeError; (eval);512; can't modify frozen object><br /># => (eval);512<br /><br /></span><span class="syntaxdefault">module FooBar</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'Hello evil world'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => Error; #<TypeError; (eval);512; can't modify frozen object><br /># => (eval);512<br /> </span><span class="syntaxdefault"></span>
-
hm... I'm tempted of adding a script that loads first that freezes the base ruby and SketchUp classes and modules just to see what happens. Could be useful to catch misbehaving plugins.
...hmm.... not sure what happens if you sub-class a frozen class....
-
@thomthom said:
...hmm.... not sure what happens if you sub-class a frozen class....
<span class="syntaxdefault"><br />class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'arrrh!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => nil<br /><br /></span><span class="syntaxdefault">BaseFoo</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment"># => BaseFoo<br /><br /></span><span class="syntaxdefault">class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => Error; #<TypeError; (eval);512; can't modify frozen class><br /># => (eval);512<br /><br /></span><span class="syntaxdefault">class ChildFoo </span><span class="syntaxkeyword"><</span><span class="syntaxdefault"> BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => nil<br /><br /></span><span class="syntaxdefault">x </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> ChildFoo</span><span class="syntaxkeyword">.new<br /></span><span class="syntaxcomment"># => #<ChildFoo;0x105ddb98><br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">prod<br /></span><span class="syntaxcomment"># => eeek!<br /># => nil<br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">poke<br /></span><span class="syntaxcomment"># => arrrh!<br /># => nil<br /><br /></span><span class="syntaxdefault">class ChildFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def prod</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'double-eeek!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => nil<br /><br /></span><span class="syntaxdefault">class ChildFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'cheeese!'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => nil<br /><br /></span><span class="syntaxdefault">class BaseFoo</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> def poke</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> puts </span><span class="syntaxstring">'moooo'</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;</span><span class="syntaxdefault"> end</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment"># => Error; #<TypeError; (eval);512; can't modify frozen class><br /># => (eval);512<br /> </span><span class="syntaxdefault"></span>
All good!
-
-
Here's a snippet I tried to lock down the API for debugging:
http://sketchucation.com/forums/viewtopic.php?f=180&t=48890#p439296 (new thread) -
@dan rathbun said:
Problem is that Ruby is designed to allow this by people who know what they are doing.
Yes - locking down the API out of the box can probably do more harm than anything.
But it does allow us to lock down for debugging. - See previous post.
-
@thomthom said:
hm... I'm tempted of adding a script that loads first that freezes the base ruby and SketchUp classes and modules just to see what happens. Could be useful to catch misbehaving plugins.
Problem is that Ruby is designed to allow this by people who know what they are doing.
Many of the Extended ruby Library files add needed methods to base classes, or modify some methods (RubyGems is an example that modifies the global
require
method, although I never understood why it really needed to do it.)Even though methods are objects, it is difficult to get a reference to an instance method within the class definition (very easy within an instance of the class.) So it is hard to freeze particular instance methods class-wide, without freezing the whole class or module. (Perhaps the gurus on the Ruby Forum know a trick?)
An alternative would possibly be (untested):
class Sketchup;;Group # Make a copy of method ;copy alias_method(;_copy_,;copy) def self.method_added(sym) # # callback called by Ruby when a new method is defined; # alias_method(;copy,;_copy_) if sym == ;copy # end end
EDIT:
method_added
is a class callback, so must be defined usingdef self.method_added(sym)
Advertisement