Help - turning inches into centimeters with Api Ruby Language

Hello, you can help me…I’m just beginning to learn the Ruby skup API language,
I wrote this little code to draw a parallelepiped…
when I enter the data, they are by default in inches, I would like to insert them in centimeters,
how can I do that, thank you.
Joseph - Italy.

These lists should help …

Read this …

… and reference the API class …

… and the SketchUp API additions to the core Ruby Numeric class …

Dan’s references are indispensable…
However the simple answer is your code currently set first three items of the array of defaults to numbers, or more exactly ‘integers’ [e.g. 100].
So even if the user tryies to enter a ‘float’, it will probably fail [e.g. 100.5 (or 100,5 if their locale uses a , rather than a . as the decimal-separator!)]
Also entering a ‘raw’ number means that SketchUp assumes ‘inches’ as the unit of measurement to be used, irrespective of your Model-Info’s Units settings.
To force the input to be a ‘length’ rather than a ‘number’ you must append a units suffix as follows…
for other units use say .mm or .m etc…
Note that in Ruby code the decimal separator is always a . - irrespective of the user’s locale and what they might input [which is converted as necessary…]
Once SketchUp recognizes the input as a ‘length’ you can even be working in one unit and input in another unit… so if you are working in cm entering 1000mm in the correctly formed dialg will be taken as 100cm - even entering 40" will be taken as 101.6cm !

1 Like

The Ruby interpreter does not use a localized decimal sign. I uses the American dot regardless. The localized decimal sign is only used when converting to and from strings using the SketchUp API methods.

…And that is why, as I have already explained, in that particular use - related to the user INPUTbox - it can take a comma within a float-input - but only in the right locale.
AND of course, as I explained in the very nest paragraph - in the Ruby API the decimal-separator must ALWAYS be the ‘.’ !


When using the UI.inputbox whatever class you provide in the default values it will try to convert he user input back into that value.

So if you are working with units (Lengths), then make sure the default is a Length

prompts = ['Name', 'Width', 'Height']
defaults = ['My Own Square', 5.m, 2.m]
input = UI.inputbox( prompts, defaults, 'Create Square' )
# User enters Width: 300cm, Height 4
p input
# => ["My Own Square", 118.110236220472, 157.48031496063]
p { |n| n.class }
# => [String, Length, Length]
p { |n| n.to_s }
# => ["My Own Square", "3000mm", "4000mm"]

By passing it Length classes the Length#to_s method will be used to display the unit in the current model’s units. And similar it’ll parse the input similar to how the VCB parses lengths.

Thank you very much for your help…

1 Like