Leaving out one field in inputbox ???+
-
I have this code working well:
prompts = ["Trin", "Gevind"] values = [@steps, @turns] results = inputbox prompts, values, "Angiv antal;" temp1, temp2 = results if (temp1 != false) @steps = temp1 @turns = temp2 steps = eval(@steps) turns = eval(@turns) mo.start_operation "screw" screw(pt1, pt2, steps, turns) mo.commit_operation end
I want to leave out one of the fields, then I run this code:
prompts = ["Trin"] values = [@turns] results = inputbox prompts, values, "Angiv antal;" temp2 = results if (temp2 != false) @turns = temp2 steps = eval(@steps) turns = eval(@turns) #<<<<<<<<<<<<<<<<<< this is line 371 mo.start_operation "screw" screw(pt1, pt2, steps, turns) mo.commit_operation end
and now I get this warning and error:
(eval):1: warning: Float 24.0 out of range
Error: #<TypeError: can't convert Array into String>
C:/Program Files/Google/Google SketchUp 8/Plugins/KSscrew.rb:371:ineval' C:/Program Files/Google/Google SketchUp 8/Plugins/KSscrew.rb:371:in
create_geometry'
C:/Program Files/Google/Google SketchUp 8/Plugins/KSscrew.rb:271:in `onLButtonDown'WHAT THE HECK IS WRONG HERE ?
No problem in the first batch of code - an array with only 1 item should be allowed or .. ?
-
eval()
tries to evaluate a string but complains@turns
is no string.
[Edit: I found @turns]
@turns
is= temp2
which is= results
. But results returns an array with in this case 1 element.
The problem is that you earlier usedtemp1, temp2 = results
temp1, temp2 = 1,2
βtemp1 = 1; temp2 = 2
**temp1, temp2 = *[1,2]**
β**temp1 = 1; temp2 = 2**
temp1, temp2 = [1,2]
βtemp1 = 1; temp2 = 2
But:
temp1 = 1
βtemp1 = 1
**temp1 = *[1]**
β**temp1 = 1**
temp1 = [1]
βtemp1 = [1]
Do you see what works in both cases? -
Oh, maybe I was not clear enough - of cause I FIRST run the first code - and it works fine !
Then I comment out the first code and made the new part of code with the SECOND code and when I run this I get the warning and error !
You write "The problem is that you earlier used temp1, temp2 = results"
I did NOT used the line 'temp1, temp2 = results' in combination with second code sample - it's commented out and I made a new line 'temp2 = results' as you can see in the second sample code.
I don't get what you mean by this:
@unknownuser said:
"> temp1, temp2 = 1,2
β temp1 = 1; temp2 = 2temp1, temp2 = *[1,2]
β temp1 = 1; temp2 = 2
temp1, temp2 = [1,2]
β temp1 = 1; temp2 = 2But:
temp1 = 1
β temp1 = 1
temp1 = *[1]
β temp1 = 1
temp1 = [1]
β temp1 = [1]
Do you see what works in both cases?" -
Why not use
temp2 = results[0]
ORtemp2, = results
instead oftemp2 = results
('results' is always an array - here with one element in your example inputbox], NOT the element itself... and then you can miss out all of the unneeded 'eval' farrago...So the 'inputbox' always returns an 'array' - or 'nil' if the user cancels.
So simply make your references to the elements of that array [even if you expect only one element] - there are several ways to read/manipulate arrays... -
@aerilius said:
The problem is that you earlier used temp1, temp2 = results
First example is earlier than the second example
Again, the inputbox returns an array, no matter if we are in the first example (where you feed the inputbox with arrays with 2 elements) or in the second example (where you feed the inputbox with arrays with 1 element).
If you use the assignment operator with two or more references and an array, it works, but it's inconsistent (Ruby should not allow it to work!):
var1, var2 = ["string1", "string2"]
It is logical that var1 will be "string1" and var2 will be "string2"
var1 = ["string1"]
Now it's also logical that var1 is an array and not the element that the array contains.It's much more consistent and reliable if you use the same object type on both sides of the
=
assignment operator, either
var1, var2 = "string1","string2"
or (the same)
var1, var2 = *****["string1","string2"]
The*
(splat/unary/asterisk) operator destroys an array into a list of objects.
So in your code it should work with
temp1, temp2 = *results temp2 = *results
Also I think
eval()
would raise a Ruby error if a user inserts something wrong. If you use.to_f
,.to_i
or a regular expression, you get exactly what you want from the user input or just nothing, but no error. Use@steps.to_f
or@steps[/[0-9\.\-]+/].to_f
-
Thx to Aerilius - very usefull explanation !
Advertisement