Ruby for the Newbie
(Page 1 of 4 )
In last week's article, we installed Ruby on Rails the easy way, by first installing Ruby, then installing Rails. In this article, the second part of a six-part series, we'll actually do something with what we installed. We'll start with an offbeat version of the Hello World program, and move forward from there.
In the bleak beginnings of 1999, three people -- Jason Fried, Carlos Segura, and Ernest Kim -- sat twiddling their thumbs and watching the night sky. All armed with shortwave satellites, they dutifully listened for extra-planetary sounds. As they shivered in the cold wind, they heard a small blip, and then another. Eventually the blip changed to static and the static changed to a voice: If you build it, nerds will come.
The trio looked at each other in awe. For months they had been listening for the 37 radio telescope signals hailed by famed astrologer, Paul Horowitz, as being the most likely source for messages from extraterrestrials. And so they listened intently as other-worldly creatures went in depth on how to create Ruby on Rails web applications.
And when they finished building the web application framework, one thing happened: nerds did come.
At least that is how my imagination plays the scene (only in my head the nerds that come all have glowing fingertips as they type on their keyboards and mutter ouccccch.) In reality it probably occurred a little differently, although the trio did invent Ruby on Rails after creating their company 37signals (which really was named for Paul Horowitz's 37 radio telescope signals).
In this tutorial, I will teach you the basics of programming with Ruby on Rails. I will make several assumptions:
You have Ruby on Rails installed and configured.
You have a database system installed, such as MySQL.
You have a .text editor such as Notepad.
If you answered no to any of those, put this tutorial down and get to installing. If you answered yes, then feel free to read ahead...
Jumping Right In
If you have ever read a programming tutorial before, you know that writers like to use the Hello World Program as the first program you develop. In reverence to our brave and intrepid Ruby on Rails creators, we are going to write a program that mimics the conversation an alien will like have with us upon first contact.
puts "Greeting Earthlings. Before you take us to your leader...
puts "Take us to your restroom." # prints the text to the monitor
This will display the text: "Greetings Earthlings. Before you take us to your leader...Take us to your restroom." on your monitor.
Yes, that is a good example of what the aliens will say to us. I mean, they traveled across an entire galaxy to meet with us. The puts command tells the program to literally put your text on the screen. Simple as pie, right?
What about numbers you say? What if aliens come but they only speak in binary? That too is simple:
puts 01001101101001 # prints the numbers onto the monitor
As you can see there is no difference between the two. You may however notice that I wrote # prints the numbers onto the monitor. In Ruby, you can leave comments for other programmers or yourself to refer to in case you ever have to go back and touch up your program. You do so by using the # symbol. When the computer sees this symbol, it ignores everything after it.
Next: A Few Notes About Formatting >>
More Ruby-on-Rails Articles
More By James Payne