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

How to break out of a Ruby loop?

Scheduled Pinned Locked Moved Developers' Forum
7 Posts 4 Posters 1.7k 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.
  • C Offline
    chrisglasier
    last edited by 27 Jan 2010, 08:50

    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
    • T Offline
      thomthom
      last edited by 27 Jan 2010, 09:01

      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
      • C Offline
        chrisglasier
        last edited by 27 Jan 2010, 09:20

        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
        • H Offline
          honoluludesktop
          last edited by 27 Jan 2010, 09:26

          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
          • T Online
            TIG Moderator
            last edited by 27 Jan 2010, 09:38

            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
            • C Offline
              chrisglasier
              last edited by 27 Jan 2010, 10:38

              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
              • C Offline
                chrisglasier
                last edited by 27 Jan 2010, 11:38

                @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
                1 / 1
                • First post
                  1/7
                  Last post
                Buy SketchPlus
                Buy SUbD
                Buy WrapR
                Buy eBook
                Buy Modelur
                Buy Vertex Tools
                Buy SketchCuisine
                Buy FormFonts

                Advertisement