# How to initialize a variable in Ruby as a length?

**URL:** <https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929>\
**Category:** Ruby API\
**Created:** [February 10, 2017, 10:55pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929 "2017-02-10T22:55:13Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 10, 2017, 10:55pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/1 "2017-02-10T22:55:13Z")

</div>

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`

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [February 10, 2017, 11:05pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/2 "2017-02-10T23:05:20Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 10, 2017, 11:06pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/3 "2017-02-10T23:06:14Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![TIG](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/tig/32/198_2.png) [@TIG](https://forums.sketchup.com/u/TIG)\
**Post date:** [February 10, 2017, 11:08pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/4 "2017-02-10T23:08:12Z")

</div>

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 ?

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 10, 2017, 11:13pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/5 "2017-02-10T23:13:30Z")

</div>

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

```ruby
      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.

---

<div class="post-metadata">

**Author:** ![john\_mcclenahan](https://sea2.discourse-cdn.com/sketchup/user_avatar/forums.sketchup.com/john_mcclenahan/32/135377_2.png) [@john\_mcclenahan](https://forums.sketchup.com/u/john_mcclenahan)\
**Post date:** [February 10, 2017, 11:25pm UTC](https://forums.sketchup.com/t/how-to-initialize-a-variable-in-ruby-as-a-length/38929/6 "2017-02-10T23:25:54Z")

</div>

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.
