Error trapping in `inputbox` for invalid lengths entered as text - example code doesn't seem to work for me

I can’t find anything later related to my topic title than this example from about five years ago.


And the code I copied from there doesn’t seem to work for me.

I want to validate user input as a valid SU length.

I’ve tried using this code to replace my inputbox, but while there is an error showing in the Ruby console, it isn’t coming from the replacement inputbox, but from later in the code where I try to convert the (invalid) input into a length.

def inputs(*args)

  results = nil
  begin
    results = UI.inputbox(*args)
puts "Results from inputs = " + results.inspect
  rescue ArgumentError => error
    UI.messagebox error.message
    if args.length > 0
      retry
    end
  end
  results
end

I added the puts line just to make sure that my active code IS using this replacement for the inputbox. It is.

If I deliberately enter an invalid length as 40g in the second (text) field expecting a length value (the default set is 0.inch), the code isn’t triggering the messagebox but just shows this in the Ruby console while closing the inputbox on screen.

Results from inputs = ["14'", "40g", "4'", "0\"", "1\""]
Error: #<ArgumentError: Cannot convert "40g" to Length>
/Users/JohnWMcC/Library/Application Support/SketchUp 2020/SketchUp/Plugins/scenery.rb:161:in `to_l'

And that’s from a much later line where I am converting the value retrieved from the text box to length using .to_l

Does that mean I have to do the error trap at that point, since the inputs method replacing inputbox isn’t trapping the error there?

It looks like all of the default values are String objects. Inputbox will return the same type as the default type you provide. What happens if you use a Length as the default type?

In other words, if you expect an input to be an Integer, use an Integer as the default value. If you expect a Float, use a Float, etc.

1 Like

I thought I had. E.g. 14.feet in drop down list, or 0.inch in the custom height field.

And in starting defaults as well as default values from last use.

As all my fields (drop down lists or custom values) are lengths, I though I might put the test in the results method instead, iterating through the fields. Would that work - trying to convert custom values to length and picking up invalid ones there using Argument Error?

They get displayed as text though, as feet & inches for (e.g., 30”) or inches, depending on Units - Architectural or Fractional.

Here is what my return array looks like:

defaults = [1, 2.2, 3.feet, "four"]
p defaults.collect{|e| e.class} # --> [Integer, Float, Length, String]
res = UI.inputbox([:a, :b, :c, :d], defaults) 
p res.collect{|e| e.class} # --> [Integer, Float, Length, String]

All of the return values from your example look like strings.

They certainly convert to strings, I agree. Will experiment some more shortly - just eating now. Thanks for the advice, Jim.

John, it will help for you to review Thomas’ blog …

Thank you again for that, Dan. I knew of it, and read it ages ago, but had lost the reference. I’ll study it again. The annoying thing I think I remember solving this some years ago, but don’t know where I put the solution.

Will keep digging.

Thanks to both @jim_foltz and @DanRathbun for your help. You pointed me in the right direction to look

I’ve found the problem. Although I set the defaults initially to lengths, when I’m saving the results as new defaults, I’m wrongly now not converting them back to lengths.

In a slightly earlier version, I had converted the accepted inputs into lengths, but then reverted to strings, or changed it back deliberately for the wrong reasons.

I CAN now trigger an error with an invalid customHeight input, and pop up the messagbox in the inputs method itself. I think I should now be able to fix the rest of the inputs.

The references (2 of them, ie, in the Ideas and the Reference lists) are in my Ruby Learning Resources that are pinned to this category. ( That’s where I went to grab the link to post it. :wink: )

I should have known that, but forgot - I’ll use it when I need something and can find it there.

I’m still wondering how to write an extension that will read your mind and tell you what you need to know before you realize you need to know it. :smiley:

:magnet::brain: :stethoscope: :level_slider: :loud_sound: :studio_microphone: :control_knobs: :computer: :calling:

Now that WOULD be magic.

Where would you get your inputs over the internet? I don’t think we have all the ioT sensors developed yet for that!

But the AI specialists are working on it, and some can convert brain signals to robotic orthotics to move a disabled person hand or arm.

But for coders…? Not the same market, perhaps.