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 />

For the impatient: If you prefer to just get things going <strong>with</strong>out learning the basics of<br />

the language first, then skip this section and go to some of the code samples in the next section<br />

(starting on page 245). Any of them work as they are when you run them in <strong>SketchUp</strong>.<br />

You can always come back later and read up on the basics here.<br />

236<br />

How Does Ruby Work?<br />

Objects, Objects, Objects<br />

Ruby is essentially an object-oriented language. Everything in the language is an object, and<br />

every object has properties and methods. You can always apply any appropriate method to<br />

an object. You can even add more methods, if you like. Here’s what I mean.<br />

Assume you have an object an_object and the object has a method a_method that does<br />

something <strong>with</strong> it. You can then apply the method to the object simply by adding it <strong>with</strong> a<br />

dot as in an_object.a_method. If you want to apply more than one method, you can simply<br />

chain them, as in an_object.a_method.another_method (as long as they are compatible).<br />

Here are some examples that make this a bit clearer.<br />

“Hello World!” is a string, as we found out earlier. It is also a String object that has some<br />

built-in methods. One of those is length. You can therefore enter “Hello World!”.length,<br />

which yields 12, the length of the string in characters.<br />

An example for chaining methods is 125.to_s.length, which gives the result 3. Now,<br />

what happened here? We started <strong>with</strong> 125, which Ruby automatically interprets as a number<br />

(an integer, actually). We then applied a conversion method to it (to_s), which converts<br />

the number object to a string. Finally, we added the length method that tells us the length<br />

of the string in characters, which, of course, is 3.<br />

A similar operation in <strong>SketchUp</strong> is converting from one unit system to another. As you<br />

know by now, internally <strong>SketchUp</strong> uses inches as its base unit, no matter which localized version<br />

you are using. If you want to convert a number in inches to, say, centimeters, you can<br />

simply use the built-in numeric function to_cm to get the correct value. Therefore, entering<br />

1.to_cm results in 2.54.<br />

As you learn more about methods, you will likely come across these two strange-looking<br />

types of methods (shown here as examples):<br />

NN<br />

NN<br />

String.upcase!—A method <strong>with</strong> an exclamation mark at the end modifies the original<br />

object in place (in this example, we turn a string into uppercase). Therefore, you don’t<br />

need to assign it to another variable.<br />

String.include?—A method <strong>with</strong> a question mark checks something on the object and<br />

results in a true or false (Boolean) answer. The example method shown here checks<br />

whether a string includes a substring.<br />

One important principle to understand about object-oriented programming languages like<br />

Ruby is that objects (and the classes that define them) have one or more ancestors (parents).<br />

Analogous to the way we receive our parents’ genes, objects in Ruby receive (inherit) their<br />

parent’s methods and properties. For example, the Edge object in <strong>SketchUp</strong>, whose parent is<br />

the Drawingelement object, has a bounds property that it inherits from that object, although<br />

by itself it doesn’t have this property (as you can see in Appendix C). This may sound confusing<br />

at first, but you will soon find that it is a very useful principle.

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

Saved successfully!

Ooh no, something went wrong!