sketchucation logo sketchucation
    • Login
    🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

    Iterators in Sketchup Ruby

    Scheduled Pinned Locked Moved Developers' Forum
    5 Posts 4 Posters 2.4k Views 4 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.
    • D Offline
      Dragon3dgraff
      last edited by

      Hello!
      I'm new here, I'm new in Sketchup, I'm new in Ruby... But I'm not new in programming.
      So, I have one maybe too stupid question.
      I'm trying to use iterators like 'for in', until and .each. But they don't work! I think this is simpliest things in any programming language... and they work in all languages I use...
      What's wrong with my Sketchup Ruby?
      I use Sketchup 8 free version


      IMG_20171220_154656_131.jpg


      IMG_20171220_093245_678.jpg

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

        It'd be much easier for us to understand - and reply - if you included actual text in the body of your post [copy+paste and use code-formatting tags].
        Your screen-shots are not the most user friendly method...

        The iteration methods you desire are available in Ruby.
        e.g. https://www.skorks.com/2009/09/a-wealth-of-ruby-loops-and-iterators/
        Most authors use them very frequently...

        To comment briefly on your issues...

        (o..n-1).each.....
        

        Where did that ' o' come from?
        I assume you meant ' 0'

        Other similar methods are:

        n.times.....
        

        etc, read up on the Ruby iteration methods, there are many !

        Also don't use capital letters to start a 'variable' [aka 'reference'] name.
        Ruby assumes those are 'Constants' - they can still be reset, but then error messages appear in the Console.
        Use lower-case characters - e.g. xxx - to start normal references.
        Capitals - XXX - for Constants that do not change.
        use @xxx for references which are shared across methods in a Module , or those in an instance of a Class.
        Use @@xxx for references remembered across instances of a Class.
        Note that $xxx variables should be avoided, they apply globally, and are reserved to the core kernel use, and their use can always be replicated using @/@@ references anyway, limiting it to within your own 'name-space' module...

        TIG

        1 Reply Last reply Reply Quote 0
        • D Offline
          Dragon3dgraff
          last edited by

          Hi, TIG!
          Thank you very much for your detailed answer, it is very usefull for me because I'm just a beginner in Ruby.
          I'm sorry for bad pics. I post new

          But... about this problem. I just copy code from this book
          Automatic SketchUp Creating 3-D Models in Ruby by Matthew Scarpino 2010
          It uses SketchUp 7.1 application programming interface (API)

          for count1 in 0..5 print count1, " " end

          This code doesn't work in Sketchup 6, Sketchup 8, Sketchup 8 PRO. But it works in Sketchup 2015PRO...


          code.JPG


          Mistake.JPG


          book1.JPG


          sketchupversion.JPG

          1 Reply Last reply Reply Quote 0
          • S Offline
            slbaumgartner
            last edited by

            The error message is saying that the interpreter parsed the text "for count1 in 0..5" and didn't find a line ending that it could understand. It wanted "do", ";", or "\n" (LF) and got something else. The fact that it works in 2015 suggests to me that this is an issue with the Ruby version embedded in SketchUp. Before SketchUp 2014 it was a variant of Ruby 1.8. As of 2014 it changed to Ruby 1.9 and since to successive versions of Ruby 2.x. One of the most important parts of the change was to require unicode character encoding. At least one of your screens suggests your computer locale is Russia? If so, your computer may be feeding unicode line endings that the older Ruby versions don't understand.

            1 Reply Last reply Reply Quote 0
            • D Offline
              driven
              last edited by

              try a 'one liner' directly in SU's Ruby Console...

              for count1 in 0..5; print count1, " "; end
              

              I think the errors are caused by RubyConsolePro rather than the snippet...

              on a PC, SU's Ruby Console has only recently accepted 'multiline' input...

              Mat always saves the snippets as files and loads those...

              john

              learn from the mistakes of others, you may not live long enough to make them all yourself...

              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