Other Pages




The following operations work on numbers:

LAB: Playing With Numbers

Answer the following questions using irb:

Order of operations

Q: What is 1 plus 2 times 3?

Order of operations

Q: What is 1 plus 2 times 3?

A: It depends!

Parentheses Are Free

When in doubt, use parentheses!

Strings vs. Numbers


1 + 2
"1" + "2"
"1 + 2"

Strings plus Numbers

Hmmm again...

"1" + 2


TypeError: can't convert Fixnum into String

The problem is that Strings and Numbers are different TYPES, aka different CLASSES.

Don't panic! The solution is easy.

Type Conversion

Numbers know a message that converts them into strings. to_s means "to string".

"1" + 2.to_s

Likewise, strings know a message that converts them into numbers.

1 + "2".to_i

to_i means "to integer".

Try this in irb!

Advanced Number Theory (optional)


The error said can't convert Fixnum into String.

Q: What is a Fixnum?

A: It's one type of number.

Math is hard

There are many types of numbers!

Each is useful in different situations.

Without getting into too much detail, the two main number types in Ruby are:

(Other number types include Complex, Rational, and Bignum.)

Number to Number

You can convert from one type of number to another by sending a message:

Try this:


String to Number

to_f and to_i also work on Strings:


and to_s works on numbers:



Try this in irb:

1 + 2
3 - 4
5 * 6
7 / 8

Whoa! What just happened?

Integer Arithmetic

7 and 8 are Integers

so the result is an Integer

7/8 is somewhere between 0 and 1

but there is no integer between 0 and 1

so the computer has to round down to 0

Floating Point Arithmetic


7.0 and 8.0 are Floats

so the result is a Float

and 0.875 can fit in a float

Okay, that's enough math!