Get length value into centimeter units

Hello, a little help…" input1 " is a value of an html cell box, managed as " id “and " value " by javascritp…
Then I turn the numeric value " input1 " into length like this…” side_x = input1 . to_l " I assign the length value to the variable " side_x "…I would like this value of length (side_x) to be transformed into units of length of centimeters…how can I do that?..Thank you.

Some help…Thank you

cm_side_x = side_x.to_cm
#=> returns Float in centimeters

… or if the model units are set to centimeters …

side_x =
#=> 10.0 # inches
puts "Side x is #{Sketchup.format_length(side_x)} long." #=> String
#=> Side x is 25.40cm long.
#=> 25.4 # Float

A good thing to read …

Thanks for the answer, I’m missing something…
input1, input2, input3… string values taken from HtmlDialog, handled by Js through "id “…
I transform string values to length with " .to_l " in latox, latoy, altezza_z (inch units)
I try to transform from inches to cm… with…” . cm "… cm_ltx, cm_lty, cm_alt_z…
but it doesn’t work, I get different values from the ones I would like to get…
I would like to get… latox is 50 cm, latoy is 65 cm, altezza_z is 250 cm, if the model’s unit of measurement
in centimeters… Or… 0.50 m, 0.65 m, 2.50 m, if unit measure model set in meters.
A few more suggestions… would be very welcome… Thank you.

Pil_1piano.rb (1.3 KB)

Pil_1piano.txt (2.6 KB)

BEWARE of the Ruby Code Editor !

When you enter statements, (or use the p or puts) the results are passed to the Length class’ inspect and / or to_s method.

The overridden Length#to_s method formats the output into a string that is expressed in the current model units.

Somehow the web-based Ruby Code Editor is intercepting the output from p(), via Length#inspect and passing it back through Length#to_s.

This fools you into thinking that the value is something other than what it really is.
You should use .inspect to inspect the real values of your variables.

You do not need to do this if your dialog is hardset to take cm, then try …

input1 = "50"
cm_input1 =
#=> 0.50m
#=> Length
# It was the .cm() call that produced a Length class object.
#=> 19.68503937007874 # in inches

Thank you very much Mr. Dan… I did as he explained to me
this the result … :stuck_out_tongue_winking_eye::stuck_out_tongue_winking_eye:


1 Like

p calls .inspect and puts call to_s.

Which works fine in the standard SketchUp Ruby Console.

I’ve edited my previous post …

I again reiterate the last sentence …

You should use .inspect to inspect the real values of your variables.

If p() is supposed to call length_object.inspect, but does not (in a web dialog based editor console,) then please try explicitly calling length_object.inspect. :roll_eyes:

Because these web-dialog based consoles have these “behaviors” that can lead coders astray, I will never recommend them, especially when testing extensions.