Namespace and Tool Class Question
-
I have started working on my wood joint tools script to wrap it in my namespace and this is what I came up with so far. The attached code that demonstrates only the Dowell part of my tools. This works, however I would like some comments on the method.
I retained the old directory structure for the actual tool K2WS_Tools\K2_ToolsDowelJoint.rb so that the new version would overwrite the old version for users that have downloaded the old version.Keith
#This Program Loads the Individual K2_Tools when their Menu is accessed! require 'K2WS/k2ws_module.rb' #-NameSpace Module module K²WS;;K2_ToolsLoader class << self #ToolBars are only created when Menu Item is selected def dowel_ToolBar if @tlb5== nil itcmd51 = UI;;Command.new("Dowel Joint") {dowelJoint} itcmd51.tooltip = "Complete Dowel Joint" itcmd51.small_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","DowelJoint_small.png") itcmd51.large_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","DowelJoint_large.png") itcmd52 = UI;;Command.new("Dowels 1 Comp") {dowel1Comp} itcmd52.small_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","Dowel1Comp_small.png") itcmd52.large_icon = File.join(File.dirname(__FILE__),"K2_ToolsSupportFiles","Dowel1Comp_large.png") itcmd52.tooltip = "Dowel 1 Comp" @tlb5 = UI;;Toolbar.new("Dowels") @tlb5.add_item(itcmd51) @tlb5.add_item(itcmd52) @tlb5.show else #state = @tlb5.get_last_state #UI.messagebox("State of Dowel ToolBar " + state.to_s) @tlb5.show end end def dowelJoint require 'K2WS_Tools\K2_ToolsDowelJoint.rb' Sketchup.active_model.select_tool K2_Tools_Dowel_Joint.new end def dowel1Comp require 'K2WS_Tools\K2_ToolsDowelJoint.rb' Sketchup.active_model.select_tool K2_Tools_Dowel_Holes.new end end # proxy class #Run Once unless file_loaded?(File.basename(__FILE__)) # Access the main Tools menu tools_menu = UI.menu "Tools" # Add a separator and a submenu tools_menu.add_separator sub_menu = tools_menu.add_submenu("KK_Tools") # Add items to the submenu it1= sub_menu.add_item("Dowels"){dowel_ToolBar} file_loaded(File.basename(__FILE__)) end #End Run Once end #module K²WS;;K2_ToolsLoader
-
Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).
@unknownuser said:
'K2WS_Tools\K2_ToolsDowelJoint.rb'
Ruby internally uses forward slashes that are common on all unix and most non-unix systems. To deal with file paths, you let Ruby handle the delimiters and use the File methods like
File.join("K2WS_Tools", "K2_ToolsDowelJoint.rb")
. Also it would be best if your plugin works independent of the installation path. Therefore define a constant in your main ruby file (or loader) in your namespace
PLUGIN_ROOT = File.dirname(__FILE__) unless defined(self::PLUGIN_ROOT)
and reference all files relative to that. -
@aerilius said:
Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).
² also exists in the ANSI set - so I could be a simple one byte character is he used that encoding.
@aerilius said:
Looks good so far. I haven't seen anyone using ² in a module name (but if it works, though Unicode characters like ᚠᚢᚦᚨᚱᚲ are not supported in our version of Ruby in SketchUp, otherwise my plugins would be full of them).
I copied the example from this page: http://www.oreillynet.com/ruby/blog/2007/10/fun_with_unicode_1.html
<span class="syntaxdefault"><br />module Kernel<br /> alias λ proc<br /><br /> def ∑</span><span class="syntaxkeyword">(*</span><span class="syntaxdefault">args</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">sum </span><span class="syntaxkeyword">= </span><span class="syntaxdefault">0<br /> args</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">each</span><span class="syntaxkeyword">{ |</span><span class="syntaxdefault">e</span><span class="syntaxkeyword">| </span><span class="syntaxdefault">sum </span><span class="syntaxkeyword">+= </span><span class="syntaxdefault">e </span><span class="syntaxkeyword">}<br /> </span><span class="syntaxdefault">sum<br /> end<br /><br /> def √</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">root</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">Math</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">sqrt</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">root</span><span class="syntaxkeyword">)<br /> </span><span class="syntaxdefault">end<br />end<br /></span>
And it worked:
` # A real lambda
λ { puts ‘Hello’ }.call => ‘Hello’Sigma - sum of all elements
∑(1,2,3) => 6
Square root
√ 49 => 7.0`
You can also do this:
<span class="syntaxdefault"><br />module MyTest<br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">ᚠᚢᚦᚨᚱᚲ<br /> puts </span><span class="syntaxstring">'world'<br /> </span><span class="syntaxdefault">end<br />end<br /></span>
But not this:
<span class="syntaxdefault"><br />module ᚠᚢᚦᚨᚱᚲ<br /> def self</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">hello<br /> puts </span><span class="syntaxstring">'world'<br /> </span><span class="syntaxdefault">end<br />end<br /></span>
Return an error:
Error: #<SyntaxError: (eval):516: compile error (eval):516: class/module name must be CONSTANT module ᚠᚢᚦᚨᚱᚲ; def self.hello; puts 'world'; end; end ^> (eval):516
That seem to be related to ᚠ not being recognised as a capital letter.
Because this works:
<span class="syntaxdefault"><br />module Xᚠᚢᚦᚨᚱᚲ</span><span class="syntaxkeyword">; </span><span class="syntaxdefault">end<br /></span>
Advertisement