No Implicit Conversion fron String in Messagebox
-
Hello.
I'm trying to print out two numbers from a script where the user inputs a wall thickness and wall height. An inputbox takes the two prompts and is supposed to spit it out on the screen. However, when I try to get the messagebox to output the two numbers, no output occurs. Instead, I get "Ruby says 0". There is no messagebox that appears
Adding a
.to_s
to each element ininputs
gives a "no implicit conversion from string"Adding a
.to_i
to each element ininputs
gives a "ruby says 0" and the messagebox does not appearI'm using Sketchup Make 2016. I wrote the script on the Ruby Code Editor
By: Alexander C. Schreyer Version: 3.1 (3/6/2013)
If you need any more information, kindly tell me.mod = Sketchup.active_model # Open model ent = mod.entities # All entities in model sel = mod.selection # Current selection Faces = [] f = mod.selection[0] #Create the inputbox prompts = ["Wall Thickness (in):", "Wall Height (ft):"] defaults = [8, 9] inputs = UI.inputbox( prompts, defaults ,'Length Input') #Set the wall thickness and wall height from inputbox to variables thickness = inputs[0].to_i h = inputs[1].to_i UI.messagebox(thickness, h)
-
@arcteo said:
Hello.
I'm trying to print out two numbers from a script where the user inputs a wall thickness and wall height. An inputbox takes the two prompts and is supposed to spit it out on the screen.
UI.messagebox accepts 1 or 2 variables. The first is a text string. The second determines what buttons appear. Within the text string, you need to enclose the thickness and height variables in curly brackets {} preceded by a # symbol as demonstrated below
UI.messagebox("#{thickness}, #{h}",MB_OK)
Option 2 would be to convert the thickness and height variables to strings and add them
UI.messagebox(thickness.to_s + ', ' + h.to_s)
-
OK! I got it to work, thanks to you!
Let me ask you: how did you learn Ruby scripting for Sketchup? Did you use books, videos, search forums? Any tips for new scripters?
-
@arcteo said:
OK! I got it to work, thanks to you!
Let me ask you: how did you learn Ruby scripting for Sketchup? Did you use books, videos, search forums? Any tips for new scripters?
I have a Ruby programming reference manual but most of what I have learned is from studying plugins done by others and visiting the Plugins and Developers forums. The Ruby API is also an invaluable tool.
http://ruby.sketchup.com/ -
Another point is that your defaults are set to be both 'integers'.
defaults = [8, 9]
and this means that only whole numbers can be entered by the user - i.e. no 4.5.
Also one default is expected in inches and the other one is in feet.
Why not set your defaults to their proper units initially?
defaults = [8.0.inch, 9.0.feet]
Then the user can enter whatever they wish and your code will automatically know that the input is in those units with no further conversions needed...
http://ruby.sketchup.com/Numeric.html
http://ruby.sketchup.com/Length.html -
TIG, I thought it said that .feet converts feet to inches? (See attachment)
But now it worked. I wonder if it was a typo or am I missing something here.
Thanks!!
-
ALL of SketchUp's internal database lengths are stored in inches, irrespective of the units you use.
In the API there are the numeric and length classes.
So if you have an input value that defaults to a 'length' - e.g.9.feet
, or any other units format - then it is always 'stored' and 'processed' in inches [9*12=108"].
However, this is flexible as the user can input in any units provided they use a units suffix: however how it's displayed in the input-box depends on the current model info > units settings...
Otherwise you need to default to a string value and then reverse-engineer that - e.g.
h="9"
[read from theinput[1]
]
which you then reset for later use as a parallel value
hft=h.to_f.feet
So the input is always taken as a decimal-feet value, but then used in inches internally...
Advertisement