Ruby for Newbies: Variables, Datatypes, and Files

Ruby is a one of the most popular languages used on the web. We've recently started a new screencast series here on Nettuts+ that will introduce you to Ruby, as well as the great frameworks and tools that go along with Ruby development. Today, we'll look at the basic Ruby datatypes, as well as using variables and storing your Ruby code in files.


Catch Up


View Screencast


Ruby's Basic Datatypes and Objects

In this chapter, we'll introduce you to the basics of Ruby datatypes.

Strings

Ruby strings aren't much different from other languages you're probably used to. You can use both double quotes and single quotes to create strings:

Of course, if you want to use the same type of quote within the string, you'll have to escape it:

Ruby offers string interpolation: "embedding" the result of some code inside a string. You'll probably do this most often with variables, but you can execute any code you'd like in there. The code you are interpolating goes between #{ and }:

Numbers

Ruby can handle both integers and floating point numbers (numbers with decimals), and it does it just how you would expect:

Here's the first of many pieces of syntactic sugar we'll see: you can use an underscore as a thousands divider when writing long numbers; Ruby ignores the underscore. This makes it easy to read large numbers:

Arrays

There are two ways to create an empty array:

It's easy to create an array with elements in it, as you can see.

To add an item to an array, you can use the push method, or use square-bracket notation, putting the appropriate index in the brackets. You can use square brackets to get the values back, as well.

Hashes

A hash in Ruby is like an object literal in JavaScript or an associative array in PHP. They're made similarly to arrays:

To put item in a hash, you again use the square-bracket notation. You can use any value as the key, but strings, or symbols (coming up next) are common options.

To create a hash with objects in it right from the beginning, you use a notation almost identical to JavaScript's object literals; the only difference is that Ruby uses an arrow (=>) between keys and values:

Symbols

Symbols are light-weight strings. They're often used as identifiers, in places other languages would often use strings. They're used instead of strings because they can take up much less memory (it gets complicated, so I'm trying to keep it simple; if you want to read more check out this article).

To create a symbol, simply precede a word with a colon; you can see a few examples of this in the code snippets above.

True, False, and Nil

These values work just as you'd expect them to. Nil is Ruby's "nothing" value, although it is an object.


Methods on Objects

Because everything in Ruby is an object, pretty much everything has methods that you can run:

You can easily add methods to objects. The first way is by adding a singleton method. This is a method that is put only of a single instance of an object.

If you wanted all strings to have this method, you could so this by opening the String class and adding an instance method.


Ruby Files

Of course, you'll want to put your Ruby code in files as it gets longer. Just give that file an extension of .rb, and you should be good.

Try putting this in a file:

To run this code, open your terminal and run this:


Conclusion

That's it! Let me know if you have any questions in the comments!

Tags:

Comments

Related Articles