See:
Also you can wrap your sample console output in
```text
delimiters.
puts( 'Loading Brad''s bst' ) #test msg
You cannot embed a single quote character within a single quoted string like this.
See String literals section of the Ruby Literals primer.
ie …
puts( "Loading Brad's bst" )
# or
puts( %{Loading Brad's bst} )
# or
puts( %Q[Loading Brad's bst] )
# ... etc., ...
Please do not put a space between the method identifier and it’s parameter list.
This will cause warnings output to the console and/or warnings from Rubocop if you are using it.
Ruby uses 2 space indents. Using larger indentation causes readers of your code to do excessive horizontal scrolling when you post in the forum.
Also many forums and GitHub will use 8 spaces for TABs. So it’s best to have your code editor automatically replace TABs with 2 spaces for .rb
files. (Good editors allow different settings for different coding languages. (4 for .py
files, 8 for .c,.cpp,.h
, etc.)
Ruby does not force the use of frivolous parenthesis around each conditional expression. Ie:
unless args.length > 0 && args[0].is_a?(Sketchup::ComponentInstance)
The convention is to use them only when it is necessary to force order of evaluation.
Of course you do, because literal strings must be enclosed in some kind of quotation.
But it is acceptable to set a reference to a string and use this reference in the call.
comp = 'test'
m(comp, [10,10,10])
Because you did not properly quote the string, the Ruby interpreter looked for an object identified with the reference test
and found it was the global method #test
, from module Kernel
, which is mixed into class Object
which is the top level ObjectSpace
called main
. (Ruby is implemented in C so it’s interpretive program loop is the C function main()
.)
So, because Kernel#test
has a minimum of 2 and maximum of 3 arguments and your statement …
m test, [10,10,10]
… did not pass any arguments in a parameter list with test
(ie, it was followed immediately by a comma,)
…
you get an ArgumentError
that correctly informs you that you passed 0 args instead of between 2…3 to the method test
(the method name always follows “in” for error messages.)
The second blank “:in ‘’ SketchUp” means it was at the top level. (Normally this would give the Ruby filename followed by the line number.)
Incorrect nomenclature. A Ruby tool is a code object that implements abstract SketchUp::Tool
class interface. It is a tool that the user interacts with using the cursor (usually a tool specific cursor icon or icons,) in order to manipulate the model.
What you describe is more of a utility. A command that runs, does it’s thing and finishes.
Your not the first to try this. However, SketchUp Ruby is a shared environment. Ruby’s top level namespace is a special instance of Object
. Everything in Ruby is an object, therefore a subclass of Object
. Whatever you define at the top level becomes global and every other object (classes and modules) of Ruby’s and the SketchUp API and all other author’s extension objects will inherit what you define at the the top level.
So everything (using your example) is going to inherit a m()
method. This is unacceptable in a shared environment.
You are going to run into clashes when you try to define a p()
, pp()
, j()
and jj()
methods because these are already globally defined. If you defined these you redefine them and can cause havoc with other extensions.
This is why the number 1 rule of SketchUp coding is stay within YOUR namespace module.
Every author needs to invent a unique top level namespace module name. Your name or your company’s name. Then each of your extensions need to be separated into it’s own submodule inside your top level namespace module, so they do not interfere with one another.