[Code] Set class fix v0.3.2

[Code] Set class fix v0.3.2

Postby Dan Rathbun » Sat May 19, 2012 4:21 pm

    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
    Code: Select all
    #  ----------------------------------------------------------------
    #  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.

    -
    0
    Last edited by Dan Rathbun on Sat Mar 01, 2014 7:09 am, edited 7 times in total.
      I'm not here much anymore. But a PM will fire email notifications.
      User avatar
      Dan Rathbun 
      PluginStore Author
      PluginStore Author
       

      Re: Set class fix (beta)

      Postby Dan Rathbun » Sun May 20, 2012 6:07 pm

      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 ??
      0
        I'm not here much anymore. But a PM will fire email notifications.
        User avatar
        Dan Rathbun 
        PluginStore Author
        PluginStore Author
         

        Re: Set class fix (beta)

        Postby thomthom » Mon May 21, 2012 9:10 am

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


        if RUBY_PLATFORM =~ /(darwin/ # Mac / OSX
        Isn't that regex invalid?
        0
        Thomas Thomassen — SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund
        User avatar
        thomthom 
        PluginStore Author
        PluginStore Author
         

        Re: Set class fix (beta)

        Postby Dan Rathbun » Mon May 21, 2012 8:04 pm

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

        Yup fixed it.. thanx.
        0
          I'm not here much anymore. But a PM will fire email notifications.
          User avatar
          Dan Rathbun 
          PluginStore Author
          PluginStore Author
           

          Re: Set class fix (beta)

          Postby Dan Rathbun » Tue May 22, 2012 3:03 am

          thomthom wrote: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.
          0
            I'm not here much anymore. But a PM will fire email notifications.
            User avatar
            Dan Rathbun 
            PluginStore Author
            PluginStore Author
             

            Re: Set class fix (beta)

            Postby Dan Rathbun » Sat May 26, 2012 6:42 pm

              Update to v0.3.0
              0
                I'm not here much anymore. But a PM will fire email notifications.
                User avatar
                Dan Rathbun 
                PluginStore Author
                PluginStore Author
                 

                Re: [Code] Set class fix (beta) v0.3.1

                Postby Dan Rathbun » Sat May 26, 2012 8:23 pm

                  Update to v0.3.1
                  0
                    I'm not here much anymore. But a PM will fire email notifications.
                    User avatar
                    Dan Rathbun 
                    PluginStore Author
                    PluginStore Author
                     

                    Re: [Code] Set class fix v0.3.2

                    Postby Dan Rathbun » Sat Mar 01, 2014 7:11 am


                    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.

                    :!:
                    0
                      I'm not here much anymore. But a PM will fire email notifications.
                      User avatar
                      Dan Rathbun 
                      PluginStore Author
                      PluginStore Author
                       

                      Re: [Code] Set class fix v0.3.2

                      Postby tt_su » Mon Mar 03, 2014 2:58 pm

                      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.
                      0
                      User avatar
                      tt_su 
                      SketchUp Team
                      SketchUp Team
                       

                      SketchUcation One-Liner Adverts

                      by Ad Machine » 5 minutes ago



                      Ad Machine 
                      Robot
                       



                       

                      Return to Developers' Forum

                      Who is online

                      Users browsing this forum: No registered users and 11 guests