sketchucation logo sketchucation
    • Login
    ℹ️ Licensed Extensions | FredoBatch, ElevationProfile, FredoSketch, LayOps, MatSim and Pic2Shape will require license from Sept 1st More Info

    Problem with group.copy

    Scheduled Pinned Locked Moved Developers' Forum
    20 Posts 7 Posters 643 Views 7 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • R Offline
      Render Plus
      last edited by

      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.

      1 Reply Last reply Reply Quote 0
      • TIGT Offline
        TIG Moderator
        last edited by

        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' add group.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.

        TIG

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

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

          Thomas Thomassen — SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • thomthomT Offline
            thomthom
            last edited by

            @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">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">FooBar</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen object><br />#&nbsp;=>&nbsp;(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">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen object><br />#&nbsp;=>&nbsp;(eval);512<br />&nbsp;</span><span class="syntaxdefault"></span>
            

            Thomas Thomassen — SketchUp Monkey & Coding addict
            List of my plugins and link to the CookieWare fund

            1 Reply Last reply Reply Quote 0
            • thomthomT Offline
              thomthom
              last edited by

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

              Thomas Thomassen — SketchUp Monkey & Coding addict
              List of my plugins and link to the CookieWare fund

              1 Reply Last reply Reply Quote 0
              • thomthomT Offline
                thomthom
                last edited by

                @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">#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">BaseFoo</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">freeze<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen class><br />#&nbsp;=>&nbsp;(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">#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;#<ChildFoo;0x105ddb98><br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">prod<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;eeek!<br />#&nbsp;=>&nbsp;nil<br /><br /></span><span class="syntaxdefault">x</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">poke<br /></span><span class="syntaxcomment">#&nbsp;=>&nbsp;arrrh!<br />#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;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">#&nbsp;=>&nbsp;Error; #<TypeError; (eval);512; can't modify frozen class><br />#&nbsp;=>&nbsp;(eval);512<br />&nbsp;</span><span class="syntaxdefault"></span>
                

                😄 All good!

                Thomas Thomassen — SketchUp Monkey & Coding addict
                List of my plugins and link to the CookieWare fund

                1 Reply Last reply Reply Quote 0
                • TIGT Offline
                  TIG Moderator
                  last edited by

                  http://i.nt.ro/freezing-classes-and-the-nearest-thing-to-unfreezing/
                  http://www.oreillynet.com/ruby/blog/2007/04/ruby_code_that_will_swallow_yo.html

                  TIG

                  1 Reply Last reply Reply Quote 0
                  • thomthomT Offline
                    thomthom
                    last edited by

                    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)

                    Thomas Thomassen — SketchUp Monkey & Coding addict
                    List of my plugins and link to the CookieWare fund

                    1 Reply Last reply Reply Quote 0
                    • thomthomT Offline
                      thomthom
                      last edited by

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

                      Thomas Thomassen — SketchUp Monkey & Coding addict
                      List of my plugins and link to the CookieWare fund

                      1 Reply Last reply Reply Quote 0
                      • Dan RathbunD Offline
                        Dan Rathbun
                        last edited by

                        @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 using def self.method_added(sym)

                        I'm not here much anymore.

                        1 Reply Last reply Reply Quote 0
                        • 1 / 1
                        • First post
                          Last post
                        Buy SketchPlus
                        Buy SUbD
                        Buy WrapR
                        Buy eBook
                        Buy Modelur
                        Buy Vertex Tools
                        Buy SketchCuisine
                        Buy FormFonts

                        Advertisement