How to initialize a variable in Ruby as a length?


#1

I’ve just started using NetBeans as an IDE, connected to SU as described in recent posts by @Neil_Burkholder.

When I hit a breakpoint, I want to see some values as lengths, not floats.

Some variables I set via an inputBox are shown in NetBeans at the break as Lengths, but some aren’t. And some variables used internally are too, but by no means all, and I can’t quite work out why not.

I tried initialising one of the ones that is just displaying as a float by setting:
z_level = 0.feet
but it still displays as a float at the break.

Do I have to tell it to convert to a length, every time I increment it?

Like this?
z_level += this_slice_height.to_l


#2

The inputbox will expect the user’s input to be whatever the default variable is.
So if the default is say 0.feet then the user can input a length value without a unit-suffix as a plain float/number [using the current units], or any other unit with a suffix e.g. 6" or 150mm…
If the inputbox’s value is already a ‘length’ there’s no need for further conversion when using it later…


#3

It isn’t one of the inputbox variables, it’s one I set later inside the code.


#4

Without knowing how you set it how can we help ?

What is ‘this_slice_height’ ?

Then why not set it as 0.5.feet or 150.mm etc ?


#5

this_slice_height is a calculated value (also a length) set as follows (from inputbox values which are lengths)

      if this_floor_height < std_floor_height
        this_slice_height = this_floor_height
      elsif in_floor_level < this_floor_height
        this_slice_height = slice_height
      else
        this_slice_height = this_floor_height - in_floor_level
      end
      
    if floor_no > 95
      this_slice_height = [2.5.feet, this_slice_height].min
    elsif floor_no > 85
      this_slice_height = [5.feet, this_slice_height].min
    end

I’m iterating up the floors of Scoot Baker’s RiverArch project that he’s posted about here (as Newthinking2).

Then I have z_height += this_slice_height to start the next slice of the building profile.


#6

Weirdly, now, after restarting SU but without changing the code, z_level is now reporting as a length in the debugger.

But only sometimes…

Something not quite right in my logic, I expect.