Ruby-Processing

Monday. 22 March 2010. 20:00 UTC

Are you on a Mac? Ten minutes from now you will be running your first Ruby-Processing animation, mesmerized by a color shifting 3D cube rotating in space. It’s easy.


What is Ruby?

Ruby is a fairly young programming language, conceived in 1993 and first publicly released in 1995. It was created by Japanese programmer Yukihiro “Matz” Matsumoto. And if you’re running OS X you already have Ruby installed. Yup, it’s already there waiting for you. For more historical info see “Ruby (Programming Language)” on Wikipedia. Ruby gained significant popularity with the rise of Ruby on Rails. (Rails is a web application framework written in Ruby.) In fact, web searches for things having to do with Ruby will usually land you on a page that’s specifically discussing the Rails framework. But Ruby is good for more than just building Web sites. We’re about to make a spinning cube with it, right there on your Desktop!

What is Processing?

Processing is a Java-based programming framework created by Ben Fry and Casey Reas while they were studying under John Maeda at the MIT Media Lab back in 2001. (Java itself is of a similar vintage to Ruby—1995—but it may “feel older” because of its lineage and its greater circulation.) Processing is used by students, artists, designers, researchers, and hobbyists for learning, prototyping, and production. Processing boasts a healthy development community with a wealth of helpful libraries and code examples, all available for free. Whether you’re a Processing novice or hardened “P5″ warrior, the page you’ll find most useful is right here: The Processing Language API. It’s a list of all the available Processing commands with links to relevant code examples.

Ok. So what’s Ruby-Processing?

Ruby-Processing is a plugin for Ruby created by Jeremy Ashkenas that allows you to use all the libraries and commands from Processing. Why would anyone want to do this? Because Ruby is a beautiful, conceptually elegant programming language. And Processing is a simple yet powerful framework with a history and community already attached to it. This means you can immediately dive into building complex visualizations (Processing) while doing so in a programming language that will expand your mind (Ruby). And it’s easy to get started.

Install Ruby-Processing

1. Open up Terminal. It’s a program that comes with OS X and is in your Applications > Utilities folder. The quickest way to find it is to click the magnifying glass in the upper-right corner of your screen and type in “Terminal.” When it opens you’ll see a text window with a command-line prompt. (At Stewdio we prefer to use the Ocean theme in Terminal which gives you white text on a nice blue background. A little transparency is nice too. You can change these settings in Terminal’s preference window.)

2. Paste this into Terminal and hit Enter:
sudo gem install ruby-processing

3. Type in your admin password when Terminal asks for it. These programs aren’t allowed to just install themselves, so that’s why you need to enter your password. And don’t worry, you won’t be able to see your password as you’re entering it but Terminal knows what you’re typing. Wait for the download and install… should only be a minute. And that’s it. Done. You have Ruby-Processing.

Load the Code

Now that you have Ruby-Processing installed (aka RP5 for short) it’s time to write a program and run it. Or rather, why don’t you just copy the code below and past it into a text file. I recommend TextMate (it’s currently my favorite text editor) but if you can’t afford it there’s the free TextWrangler or you can just use OS X’s built-in TextEdit. For TextMate I recommend the “Blackboard” theme. (To change visuals themes click on TextMate > Preferences and go to the Fonts & Color tab. Also, Menlo-Regular at 14pt is a respectable font choice. Note: If you use TextEdit make sure you click Format > Make Plain Text to remove text formatting (like font styles, sizes, colors, etc) before saving.

class ColorCube < Processing::App load_library :opengl def setup size 640, 360, OPENGL frame_rate 30 @x_degree = 45 @y_degree = 0 @foreground_hue = 0 @background_hue = 180 color_mode HSB, 360 no_stroke end def draw background @background_hue, 360, 360, 360 lights push_matrix translate width / 2, height / 2, 0 rotate_x radians( @x_degree ) rotate_y radians( @y_degree ) fill @foreground_hue, 360, 360, 320 box 180 pop_matrix @x_degree = ( @x_degree + 1 ).modulo 360 @y_degree = ( @y_degree + 2 ).modulo 360 @foreground_hue = ( @foreground_hue + 1 ).modulo 360 @background_hue = ( @foreground_hue + 180 ).modulo 360 end end ColorCube.new :title => "My COLORFUL Cube!"

Let’s spin some 3D cubes!

1. Save this file to your Desktop as ColorCube.rb. The “.rb” file extension helps you and your computer remember that this is a Ruby program.

2. Open up Terminal. Or perhaps you still have it open from installing Ruby-Processing? No Matter. Your Terminal’s command prompt is probably working out of your Home folder. But we want Terminal to work out of your Desktop folder because that’s where we just saved our file. Copy and paste this command into Terminal and hit Enter:
cd ~/Desktop

3. Spin your cube. Again, copy and paste this command into Terminal and hit Enter:
rp5 run ColorCube.rb
And there you go. You should be looking at a color-shifting, rotating cube on a color-shifting background. Just hit Command+Q to Quit.

Conclusions

You have Ruby-Processing installed. You just ran your first “RP5″ animation. You’re probably excited, but have a lot of questions. Try experimenting: How might I make the cube larger? Maybe it has to do with the number after that “box” command? What happens if I delete the line that says “lights”? Give it a shot. Don’t worry about breaking things. If you’ve made a mistake and can’t figure out how to fix it, just paste in this sample code again. No big deal.

Again, I would direct you to the Processing Language API which lists all of the Processing commands. You will need to “Ruby-ize” these commands: Java likes to use camelCase for function names but Ruby prefers under_scores. If you run into difficulties see the RP5 Using the Processing API page for more info.

And one last thing. Remember that Terminal command we used to switch from your Home folder to your Desktop? Wouldn’t it be nice to never have to type that again? What if your Ruby-Processing programs are in a folder within a folder within another folder somewhere else on your machine? Meet ShellHere, a free little Finder add-on to solve just such a problem. ShellHere adds a button to your Finder window that will open Terminal to whatever folder location you’re currently viewing.

Ok. Good luck and happy coding.


Anchor Annotations