1. Computing

5 Must Know Features of the Ruby Language

Getting Started Programming

By

Ruby Language Feature Number One: Variables

When you're planning a party, do you create a numbered "To-Do" list? Does it make sense to pick up the food from the caterer before you've booked a the rec hall? Once you've booked that rec hall, though, how do you remember the address to give to the caterer? In its simplified form, a computer program can be thought of as a structured "To-Do" list. Each thing on the list needs to be completed to accomplish a final task, but also needs to done in a certain order.

If your computer program is your party list, than variables are the sticky notes you use to keep track of all the data you've collected. Without your sticky notes, you'd be at a loss as to how to move forward with planning. Without using variables, your Ruby program isn't able to keep track of its data either.

Ruby Language Feature Number Two: Arrays

Programs often have to manage collections of variables. Take for example, a program that manages your party information. You're bound to have party guests, each of whom must be stored in a variable and a list of which can be stored together in an array variable. That way you can access each guest via the array.

Ruby Language Feature Number Three: Hashes

Now imagine your party is a potluck event and each guest is required to bring a dish or other item. You'll still need a list of guests, which can be managed as an array, but you'll also need a way to keep track of what types of food are being brought. Additionally, you'll probably want to know who is bringing what. Using a hash to store key/value pairs allows you to store that information. Say Susan is bringing chocolate cake to your party. The "key" will be "Susan" and the variable stored and accessed by the key (the value) will be "chocolate cake."

Ruby Language Feature Number Four: Loops

Computer programs often have to perform actions more than just once. For example, if your program prints all new information added to your pot luck list, it will need to print more than just a single time. This can be accomplished using a construct known as a loop.

Ruby Language Feature Number Five: Blocks

Blocks aren't anything new, especially to functional programmers. In the Ruby language, the use of these nameless methods is widespread and the block is one of the language's key features. They're often used to abstract the more tedious and repetitive loops into a more user-friendly forms.
  1. About.com
  2. Computing
  3. Ruby
  4. Beginning Ruby
  5. 5 Must Know Features of the Ruby Language

©2014 About.com. All rights reserved.