In this lesson we'll execute some simple Ruby Script snippets inside of SketchUp and learn about the Ruby API Documentation. By the end of this tutorial, you should be able to:
- Access the Ruby Console and use it to execute code.
- Perform basic tasks on the Ruby Console using Ruby Code.
- Use the Ruby API Documentation to learn more about specific API classes, modules, and methods.
Try it Now!
Open up SketchUp and find the Ruby Console. You can access it from the Window menu. Copy this snippet and paste it into the console and press enter to execute it.
That's a pretty simple example, but it is Ruby that took that simple math expression and returned a result. That is the basis for the rest of these lessons in the Getting Started section.
Controlling the Console
The Ruby Console is also controlable from Ruby itself. Check out this simple snippet that will let you know if the console is open:
Maybe you realized that snippet is fairly useless because we already know that the console is open, since we just executed code in it. However later you'll learn how to write ruby code, save it to a file and have it get executed when SketchUp starts, or when a user activates your code via a menu item. In those cases, sometimes its useful for you as the developer to know if the console is open so that you can output messages to it.
Now that we've executed a few things, the console is starting to get full of code and messages that sometimes get in the way. Here is how you would clear the console:
Here's a helpful tip, you can also type
cls into the console and execute that command to clear the conolse. Its not a Ruby method, so you can't use it inside of a Ruby file. It will only work when typed directly into the Ruby Console.
And lastly, you can also show or hide the console. Since you've got it open, lets go ahead and hide it. Here is what that code looks like:
Ruby API Documentation
How can you as a developer know all the methods and classes available to you? Through our Ruby API Documentation pages! For example, in this lesson you were able to interact with the Ruby Console via ruby because SketchUp adds API methods that let you do that. Get it? We have to expose control via the API, then you use the API to access anything that we have exposed control over.
You can find everything you need to know about how to manipulate the Ruby Console via ruby from the Console class page in our API docs. Go check that page out right now. You'll see all the available methods you can use to manipulate the Ruby Console, along with some sample snippets of code that begin to illustrate how to use that method.
The rest of these lessons continue using short snippets of code to execute specific tasks inside of SketchUp. You don't necessarily need to complete the lessons in any specific order, but some of the lessons will build on concepts taught in a previous lesson.