[Code] Set class fix v0.3.2
-
Here's a little script that "fixes" the conflict issue between Ruby's
Set
class and the API'sSet
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
forobj.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 therescue
block, toalias
a toplevelSet
constant, if the "set.rb" file cannot be found.0.3.0 : Changed the
insert
method from a simplealias
ofadd
, to an iteration that can handle multiple arguments like the APIinsert
method can.0.3.1 :
$0
returns aString
, 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 theSketchup
module namespace.
The SketchUp API v14 moves the APISet
class into theSketchup
namespace so it does not conflict with the Ruby standard librarySet
class. -
EDIT: Added a class alias in the
rescue
block, to alias a toplevelSet
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 ??
-
Is the Ruby Set class compatible to override the SketchUp Set class?
if RUBY_PLATFORM =~ /(darwin/ # Mac / OSX
Isn't that regex invalid? -
@thomthom said:
if RUBY_PLATFORM =~ /(darwin/ # Mac / OSX
Isn't that regex invalid?Yup fixed it.. thanx.
-
@thomthom said:
Is the Ruby
Set
class compatible to override the SketchUpSet
class?If the two method aliases for
insert
(foradd
,) andcontains?
(forinclude?
,) 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 hasEnumerable
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 subclassSortedSet
and there is anotherRestrictedSet
but it is commented out, for what reason, I do not know. -
Update to v0.3.0
-
Update to v0.3.1
-
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 theSketchup
module namespace.
The SketchUp API v14 moves the APISet
class into theSketchup
namespace so it does not conflict with the Ruby standard librarySet
class. -
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.
Advertisement