It’s a minor issue, but mildly annoying. It’s part of a plugin I use to draw scenery when designing a theatre set. The code is adapted from a very old plugin originally designed to draw timber and sheet material for woodworking designs. It’s not even an extension yet, but I’m trying to get it to function better than the original I adapted many years ago - maybe around the time of SU8? Before namespace protection and extensions vs. plugins…
I create an inputbox
in Ruby, which works fine when I click OK.
Here’s the code that creates it and processes the inputs.
prompts = ["Flat Height ","Custom height? ","Flat Width ","Custom width? ","Flat Thickness "]
if @defaultsF.nil?
@defaultsF = ["14'",'0"',"4'",'0"','1"']
end
list = ["14'|10'|8'|6'","","6'|5'|4'|3'|2'6'|2'|1'6|1'","",""]
results = UI.inputbox(prompts,@defaultsF, list, "Flat Size")
myHeight, customHeight, myWidth, customWidth, myDepth = results
customHeight = customHeight.to_l unless customHeight.nil?
customWidth = customWidth.to_l unless customWidth.nil?
myHeight = myHeight.to_l unless myHeight.nil? #Line 119
myWidth = myWidth.to_l unless myWidth.nil?
myDepth = myDepth.to_l unless myDepth.nil?
if customHeight > 0.inch
usedHeight = customHeight
else
usedHeight = myHeight
end
if customWidth > 0.inch
usedWidth = customWidth
else
usedWidth = myWidth
end
Looks like this in use:
But if I Cancel it, the Ruby console shows an error whose cause I don’t really understand.
Error: #<NoMethodError: undefined method `to_l' for false:FalseClass>
/Users/JohnWMcC/Library/Application Support/SketchUp 2020/SketchUp/Plugins/scenery.rb:119:in `createFlat'
/Users/JohnWMcC/Library/Application Support/SketchUp 2020/SketchUp/Plugins/scenery.rb:333:in `block in <top (required)>'
Line 119 is marked in the code extract above.
myHeight = myHeight.to_l unless myHeight.nil?
Why does Ruby complain about this line, rather than one just before or after?
And how does myHeight
get set to False, if that’s the issue? The unless
condition might be true - myHeight
may still be nil
if the inputbox
doesn’t process because I clicked Cancel.
But why False
in the error?
Do i need to initialise the parameters myHeight, myWidth
etc to zero or some value when I first load the plugin? If so, how?
A check for __FileLoaded?__
at the end, which would run once only and initialise things there?
Or should I set the variables to zero after the line
if @defaultsF.nil?
As I don’t normally have the Ruby console open when drawing, I won’t see the error in normal use.
But I’m trying to learn to write clean code, and expect delivery today of the 4th Edition ‘Pickaxe’ book, to help me understand better the fundamentals of Ruby. Mostly, the plugins I wrote myself for my own use were adapted (sometimes heavily) from others’ code, and others were collaborative efforts with a more experienced SU Ruby coder who did all the tricky bits (Steve Baumgartner @slbaumgartner).
Any comments or help on the code would be much appreciated.