19.12.2016 Views

Architectural_Design_with_SketchUp

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Architectural</strong> <strong>Design</strong> <strong>with</strong> <strong>SketchUp</strong><br />

Beyond IF, other control structures are UNLESS, which works similar to IF and uses the<br />

same structure, and CASE, which applies code based on variable switches.<br />

Repeating Things: FOR, WHILE, and More<br />

Whenever you need to repeat code in a loop, Ruby actually has various ways to let you do<br />

this. Let’s look at some of them. First is the “classic” for loop:<br />

for i in 1..10<br />

print i<br />

end<br />

As you can see, this uses a range (1..10) to describe how often this code should repeat.<br />

Another version is the WHILE statement:<br />

num = 1<br />

while num < 10 do<br />

print num<br />

num += 1<br />

end<br />

You can also use the elegant each method, as in:<br />

(0..10).each { |i|<br />

print i<br />

}<br />

In this form, i is assigned to each element. Although in our case this simply becomes a<br />

numeric index, we will use this extensively in <strong>SketchUp</strong> coding later to iterate through objects.<br />

Finally, the integer object also has a times method that you can employ like this. Any<br />

(multiline) code in the curly brackets will be executed 10 times in this case.<br />

10.times { |i|<br />

print i<br />

}<br />

Making It Work: Some More Ruby Syntax Details<br />

You’ve already seen that Ruby does not require you to tell it when a line ends (many other<br />

programming languages need a semicolon there). There are more language-specific things<br />

you need to know:<br />

NN<br />

A one-line comment starts <strong>with</strong> a #. Anything after the #, until the end of the line, is<br />

ignored by Ruby.<br />

240

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!