sketchucation logo sketchucation
    • Login
    πŸ€‘ SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Leaving out one field in inputbox ???+

    Scheduled Pinned Locked Moved Developers' Forum
    6 Posts 3 Posters 173 Views 3 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • K Offline
      ksor
      last edited by

      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:in eval' 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 .. ?

      Best regards
      KSor, Denmark
      Skype: keldsor

      1 Reply Last reply Reply Quote 0
      • A Offline
        Aerilius
        last edited by

        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 used temp1, 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?

        1 Reply Last reply Reply Quote 0
        • K Offline
          ksor
          last edited by

          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 = 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?"

          Best regards
          KSor, Denmark
          Skype: keldsor

          1 Reply Last reply Reply Quote 0
          • TIGT Online
            TIG Moderator
            last edited by

            Why not use temp2 = results[0] OR temp2, = results instead of temp2 = 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...

            TIG

            1 Reply Last reply Reply Quote 0
            • A Offline
              Aerilius
              last edited by

              @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

              1 Reply Last reply Reply Quote 0
              • K Offline
                ksor
                last edited by

                Thx to Aerilius - very usefull explanation !

                Best regards
                KSor, Denmark
                Skype: keldsor

                1 Reply Last reply Reply Quote 0
                • 1 / 1
                • First post
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement