May 24, 2008
More rubies than you can shake a stick at
A webdesigner ventures into the world of programming, part one
So I decided I wanted to know more about Ruby today. And I decided I would document my learning process. I am running Mac OSX 10.5 and Ruby 1.8.6. If you’re a developer, follow along and prepare for a good laugh; if you’re a designer, you might actually learn something. You will need Textmate to follow along on this one - unless you are clever enough to convert everything I say to your editor of choice.
Step one is learning something is always the same: finding a starting point. Since I didn’t want to wait for a Pragmatic Programmers‘ book to arrive I started my search online. Lucky me - they put one of their Ruby books online, for free. Hurray! Now I know these guys have quite the name in the Ruby world, so this seemed viable.
From the terminal…
Since you have Leopard and Leopard comes with Ruby, you don’t need to install anything. Easy!
Open a new terminal window and type ruby, and hit enter to put the shell into ruby mode. What this basically does is tell terminal.app that you’re running Ruby.
Good, next up is the obligatory output some text-tutorial. The classic example here is Hello World, but I like me some I CAN HAS CHEEZBURGER. So type this:
puts "I CAN HAZ CHEEZBURGER"
Nothing happens. You expected it to output the result of your code, didn’t you? Well, since most of the time you want to write multiple lines of code it would be quite stupid to execute the code on an enter keypress. You have to use a keyboard shortcut to actually execute the code.
Hit CTRL+D to execute what you just wrote. And there, your terminal outputs I CAN HAZ CHEEZBURGER. Congratulations!
Now, this is all pretty boring, we want to get real, build the next great thing, and we don’t want to work in a terminal window. We want to work in a text editor - a popular choice in the Ruby and RoR community happens to be Textmate. So download Textmate to continue if you don’t have it already. The trial runs for 30 days, and it’s pretty cheap to buy.
…to the editor
Create a new folder on your hard disk called ruby. This is going to be your project folder for learning Rubies. You can do this the classic way by going to Finder, with the mouse and all. But since we’re entering geek zone we might as well do it like the pros. Open up a new terminal window and type the following:
mkdir ruby
Yay, a new folder named ruby appeared in your home folder. Navigate into this folder:
cd ruby
Now create a textmate project from this folder: (again, you can do this by dragging the folder onto Textmate if that’s the way you roll)
mate .
A new Textmate project window opens. Create your first file inside the project and name it myprog.rb. Now repeat the same code you wrote before:
puts "I CAN HAZ CHEEZBURGER"
Now select Bundles from the top menu, go to the Ruby bundle and select run. You’re going to want to note down the shortcut for this, a simple Command + R. So hit the shortcut: this results in the following output in Textmate:
RubyMate r8136 running Ruby r1.8.6 (/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby)
>>> myprog.rb
I CAN HAZ CHEEZBURGER
Program exited.
Pretty disappointing to do nothing but a simple output text right? For my readers’ and my sake I’m hoping for something better the next time.
Note: in the original post, I spoke about an insecure world writable error, but I took all the babble about this out of the post as not to confuse future readers. This was a system-specific error that no one should encounter. Thanks Jan for pointing this out to me!