sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    [Code] Set class fix v0.3.2

    Scheduled Pinned Locked Moved Developers' Forum
    9 Posts 3 Posters 1.5k Views 3 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.
    • Dan RathbunD Offline
      Dan Rathbun
      last edited by Dan Rathbun

      Here's a little script that "fixes" the conflict issue between Ruby's Set class and the API's Set class.


      Note: This issue has been fixed in SketchUp 2014
      This file is not needed, but v0.3.2 should do nothing if loaded under SketchUp 2014.


      A full ruby install is needed.

      Testing, Thoughts etc welcome.

      One interesting thing is that instances of both classes return Set for obj.class()
      (Not sure if this is desired.)

      set_fix.rb

      
      #  ----------------------------------------------------------------
      #  File    ;  "set_fix.rb"
      #
      #  Version ;  0.3.2
      #  Date    ;  2014-03-01
      #
      #  Author  ;  Dan Rathbun, Palm Bay, FL, USA
      #  ----------------------------------------------------------------
      
      if defined?(Sketchup)
      unless defined?(Sketchup;;Set)
      
        begin
          
          # Move the API's Set class into the Sketchup module namespace;
          Sketchup;;Set = Object.class_eval('remove_const(;Set)')
          
          # Paths to standard Ruby libs must first be pushed into
          # the $LOAD_PATH (aka $;,) array;
          
          if RUBY_PLATFORM =~ /(darwin)/ # Mac / OSX
      
            # require some other script that pushes Ruby lib paths into $;
      
          else # MS Windows
      
            ['C;/Ruby186/lib/ruby/1.8',
            'C;/Ruby186/lib/ruby/1.8/i386-mswin32'].each do |lib|
              $; << lib unless ($;.include?(lib) || $;.include?(lib.downcase))
            end
      
          end
          $;.uniq! # remove duplicate entries
          
          # Now require the standard Ruby Set class via 'set.rb'
          require('set')
          
        rescue LoadError
      
          Set = Sketchup;;Set
          raise
      
        else
        
          # No errors occured, so make Ruby's Set class have
          # the two methods, that the Sketchup;;Set class has; 
      
          Set.class_eval('alias_method(;contains?,;include?)')
      
          Set.class_eval %q{
            def insert(*args)
              errors = {}
              args.each_with_index{|a,i|
                begin
                  add(a)
                rescue Exception => e
                  errors[i]= e
                end
              }
              unless errors.empty?
                stack = caller(0)
                called_from =( stack.size>1 ? stack[1] ; stack[0] )
                msg = "#{errors.size} errors occured during Set.insert() from #{called_from};\n"
                errors.each {|i,e|
                  msg<< "arg #{i} (#{args[i].class.name}); #<#{e.class.name}; #{e.message}>\n"
                }
                raise(RuntimeError,msg)
              end
            end # def insert()
          }
      
        end
        
      end # unless Sketchup;;Set class is defined.
      end # if Sketchup namespace is defined
      

      EDIT:

      0.1.0 : first post.

      0.2.0 : Added a class alias in the rescue block, to alias a toplevel Set constant, if the "set.rb" file cannot be found.

      0.3.0 : Changed the insert method from a simple alias of add, to an iteration that can handle multiple arguments like the API insert method can.

      0.3.1 : $0 returns a String, so comparison must be: $0 == 'SketchUp' (The Argument was not quoted.)

      0.3.2 : In Ruby 2.0 under SketchUp 2014, $0 returns a "-e", instead of "SketchUp" so that test was removed. We now just test for the Sketchup module namespace.
      The SketchUp API v14 moves the API Set class into the Sketchup namespace so it does not conflict with the Ruby standard library Set class.

      I'm not here much anymore.

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

        EDIT: Added a class alias in the rescue block, to alias a toplevel Set constant, if the "set.rb" file cannot be found.

        Now 1 issue is that this script can only run once, as it is now.

        Thoughts ??

        I'm not here much anymore.

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

          Is the Ruby Set class compatible to override the SketchUp Set class?

          if RUBY_PLATFORM =~ /(darwin/ # Mac / OSX
          Isn't that regex invalid?

          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:

            if RUBY_PLATFORM =~ /(darwin/ # Mac / OSX
            Isn't that regex invalid?

            Yup fixed it.. thanx.

            I'm not here much anymore.

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

              @thomthom said:

              Is the Ruby Set class compatible to override the SketchUp Set class?

              If the two method aliases for insert (for add,) and contains? (for include?,) are added to the Ruby Set class.

              The Ruby Set class has all of the other (and there is not that many,) methods of the API class.
              In addition the Ruby class has Enumerable mixed in, so it's so much more robust.

              The main difference would be speed. The API class is actually an exposure of a C++ collection.

              The Ruby class is actually a custom wrapper of a Ruby Hash. So most methods called, will in turn call some instance method on the internal @hash object.
              It also adds a subclass SortedSet and there is another RestrictedSet but it is commented out, for what reason, I do not know.

              I'm not here much anymore.

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

                Update to v0.3.0

                I'm not here much anymore.

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

                  Update to v0.3.1

                  I'm not here much anymore.

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


                    Note: This issue has been fixed in SketchUp 2014
                    This file is not needed, but v0.3.2 should do nothing if loaded under SketchUp 2014.


                    Update to 0.3.2

                    In Ruby 2.0 under SketchUp 2014, $0 returns a "-e", instead of "SketchUp" so that test was removed. We now just test for the Sketchup module namespace.
                    The SketchUp API v14 moves the API Set class into the Sketchup namespace so it does not conflict with the Ruby standard library Set class.

                    ❗

                    I'm not here much anymore.

                    1 Reply Last reply Reply Quote 0
                    • tt_suT Offline
                      tt_su
                      last edited by

                      I would recommend using the Standard Library Set class over the one in SketchUp if you can. It's better features and much faster.
                      We might be deprecating the Sketchup::Set class eventually. With the standard lib it doesn't make any sense for anything other than backward compatibility.

                      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