Expand All

Datatypes

Goals

  • Know the basic data types

  • Know why it matters

  • Variables are easygoing about the objects they hold. You can assign a word, then assign a number, then a list of words.

    But sometimes it matters what type of data we assign a variable. The fancy word for "type of data" is class.

    Ruby has several built-in classes. In a later section, we'll explore how to make your own classes.

Step 1

How can you find out what class something is?

Type this in irb:
"Letters and words".class
Expected result:
String

"class" is a method, a behavior you can ask an object to perform.

To call a method on an object, type the object or variable name, then a period, and finally the method name.

Here's a preview of the datatypes we'll cover:

Type this in irb:
'a'.class
5.class
3.14.class
true.class
:symbol.class
[ 1, 2, 3].class
{name: "value"}.class
nil.class
Class.class

Step 2

Different classes come with different methods. Browse the String class' methods. (Ruby will put a ':' before the method name.)

Type this in irb:
"five".methods

There are a lot of them. Can you predict what some of them do?

Numbers have different methods.

Type this in irb:
5.methods

Notice that number methods include arithmetic operators.

Knowing what class an object is tells us what methods we can call.

Step 3

Some datatypes are incompatible. For example, Ruby knows how to concatenate two objects of the same class.

Type this in irb:
5 + 5
"5" + "5"

Ruby can guess how to convert some classes into a similar class.

Type this in irb:
5 + 5.0

... but it's totally confused by different objects.

Type this in irb:
5 + "5"
"5" + 5

Step 4

When you have incompatible data types, you can use an object's conversion methods to get a translated version of the object.

Conversion methods can be spotted by their name; they begin with to_, e.g. to_s converts to a string, to_i to an integer, and to_f to a float (decimal).

Type this in irb:
5.to_s
5.to_s.class
5.to_f
5.to_f.class
5.67.to_i
"5".to_i
"5".to_i.class

There isn't always a sensible conversion from one class to another.

Type this in irb:
"I am not a number".to_i
"five".to_i

Conversion methods are used most often in calculations and in printing.

Type this in irb:
5 + "5".to_i
"5 plus" + 5.to_s

It's common to include a to_s method when you make your own classes so it prints nicely.

To explore further, try converting the examples in step 1 to different classes. To more easily see what conversion methods an object has, call methods.sort on it.

Type this in irb:
5.methods.sort

Explanation

Sometimes it's necessary to think about what type of data you're working with.

Use the class method to find out what class an object is.

Use the methods method to find out what methods an object has.

Use conversion methods to get a transformed version of an object.

A TypeError message is a hint to look more carefully at an object's class.

Let's look at the basic datatypes in more detail.

Next Step: