How to break out of a Ruby loop?
-
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
-
Not 100% sure what you're trying to do.
But is it this line:
next if pl == ""
you're testing ifpl
is a string? Because later you trypl.join
which seem to imply you expect anArray
.But as the error message says pl contains
"3D North;"
.So,
next if pl == ""
should instead benext 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, whilenext
just skips to the next iteration - which I wouldn't call breaking out of a loop. -
Thanks TT, I'll sleuth it out some more.
-
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?
-
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...
-
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
-
@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, whilenext
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 tobreak
and sometimes fornext
,redo
orretry
. Found these here.
Advertisement