Numeric Conversion

Hi.,
Usually .cm operator converts centimeter values to inches. irrespective of the unit setup in the sketchup or it will behave based on the unit setup

But i am getting output like this. 10.cm shows 100mm

Thanks

Your first numerical input is taken as inches,
so 10.to_cm returns 25.4, i.e. that’s 10" in cm
then 10.cm is taken a numerical 10 [not inches] converted into current units [mm] => 100.0mm

1 Like

For starters, you need to understand that SketchUp’s Ruby has a subclass “Length” derived from Numeric. This class mainly just tags a Numeric so that SketchUp knows it was meant to represent a length vs just being a number. The actual units of a Length are always inches.

The setting in Model Info->Units specifies the format that SketchUp will use when displaying any Length value on the GUI or taking input from the measurements box. So, for display, mm units with precision .0 tells SketchUp to display a length that internally is 10 inches by calculating its equivalent in mm and rounding to one decimal place. It does not change the internal value, just says how to format it.

Regarding the .cm method, SketchUp Ruby added this to Numeric to tell the interpreter that the string literal “10.cm” in the code is to be taken as “the raw number 10 was meant to be cm, so convert it accordingly when generating the numeric value of Length to store”. You might mentally rename the method “.from_cm”. The .to_cm method is a value conversion method. It tells the interpreter “generate the numeric value for the number of cm equivalent to this number in inches”. Note that this does not require that 10 be already a Length, just a number, and does not imply that the result is a Length.

To convince yourself of these things, try this:

10.class # -> Integer
10.cm.class # -> Length
10.to_cm.class # -> Float
2 Likes

@TIG / @slbaumgartner . Thanks for the detailed explanation.

@susketch17 Note that the output to the Ruby Console is by default run through the to_s method for the class of the object (or result.) So you see the value in model units because the result is of class Length and this class has an overridden #to_s method that displays a string in model units.

But to see the real value, use the inspect method …

10.cm.inspect
#=> 3.937007874015748

So you see that the value is actually internally in inches.

3 Likes

Thanks @DanRathbun