sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    How to break out of a Ruby loop?

    Scheduled Pinned Locked Moved Developers' Forum
    7 Posts 4 Posters 1.8k 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.
    • chrisglasierC Offline
      chrisglasier
      last edited by

      Here is a snippet of code from a callback. Someone set me straight please!

      
      pages = model.pages
      		parray = []
      		pages.each do |page|
      			pl = []
      				page.layers.each do |layer|
      					if layer.visible? == true
      						pl.push(layer.name)
      					end
      				next if pl == ""
      				pl = pl.join(",")
      				pl = [page.name,pl]
      				pl = pl.join(";")
      				parray.push(pl)
      			end
      		end
      
      

      roof004.png

      With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Not 100% sure what you're trying to do.

        But is it this line: next if pl == "" you're testing if pl is a string? Because later you try pl.join which seem to imply you expect an Array.

        But as the error message says pl contains "3D North;".

        So, next if pl == "" should instead be next if pl.is_a?(String) ?

        That's what you mean by breaking out of a loop? Bit confusing since you have the break keyword that actually breaks a loop, while next just skips to the next iteration - which I wouldn't call breaking out of a loop.

        Thomas Thomassen β€” SketchUp Monkey & Coding addict
        List of my plugins and link to the CookieWare fund

        1 Reply Last reply Reply Quote 0
        • chrisglasierC Offline
          chrisglasier
          last edited by

          Thanks TT, I'll sleuth it out some more.

          With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

          1 Reply Last reply Reply Quote 0
          • honoluludesktopH Offline
            honoluludesktop
            last edited by

            Not sure if this is the place for this question, but with a procedural language like C, you can goto label or exit n. Can you do this in ruby?

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

              ent=nil
              entities.each{|e|
                if e.class==Sketchup;;Text
                  ent=e
                  break
                end#if
              }
              

              This looks through entities until it finds a Text entity, then it stops looking with break

              ent=nil
              entities.each{|e|
                next if not e==Sketchup;;Text ### skip over current 'e' if it's NOT Text and look at next one...
                ### you add loads of code here to process 'e',
                ### as we know it's sure to be Text
              }
              

              ent=nil
              until ent
                ### when you have set 'ent' to something it stops the loop
                ### same as 'while not ent'...
              end#until
              
              

              There is no 'goto()' or 'exit()' in the same way as say Basic...

              TIG

              1 Reply Last reply Reply Quote 0
              • chrisglasierC Offline
                chrisglasier
                last edited by

                On the otherhand maybe if I tell you what I want to achieve maybe you could suggest something simpler.

                I create a new scene and then make all the components and layers visible in it. At the same time I collect the names of all components, their layers and scenes to start the associative index (nameset) in its webdialog. With this scene I can set the visibility of the components in different types of collections and individually, not restrained by components stuck in layers. Also I can structure the scenes into hierarchies and sets for printing, purchase orders, animation and so on.

                So now I have simplified the code for hoiking out scene names and their hidden layer names like this:

                
                model = Sketchup.active_model
                		pages = model.pages
                		parray = []
                		pages.each do |page|
                			pl = []
                				page.layers.each do |layer|
                				pl.push(layer.name)
                				end
                			pl = pl.join(",")
                			pl = [page.name,pl]
                			pl = pl.join(";")
                			parray.push(pl)
                		end
                		p parray.join("|")
                

                But the next step is to hoik out the names of layers which only have dimensions or notes. (Previously I was trying to do it in one loop.)

                Thanks

                Edit: no c in hoik

                With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                1 Reply Last reply Reply Quote 0
                • chrisglasierC Offline
                  chrisglasier
                  last edited by

                  @thomthom said:

                  ...

                  That's what you mean by breaking out of a loop? Bit confusing since you have the break keyword that actually breaks a loop, while next just skips to the next iteration - which I wouldn't call breaking out of a loop.

                  A bit pedantic perhaps but I am influenced by the API typo thread.

                  You need to break out of the loop before it finishes to start the next iteration. Sometimes you break out to break and sometimes for next, redo or retry. Found these here.

                  With TBA interfaces we can analyse what is to be achieved so that IT can help with automation to achieve it.

                  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