Working with Numbers
In order to start
irb open your terminal and type
irb, then hit the
return key (
enter). In order to quit
irb again (and get back to your
system shell prompt) you can type
exit or press
ctrl-d, which does the
- How many hours are in a year.
- How many minutes are in a decade?
- How many seconds old are you?
What do you think happens when you combine the following floats and integers?
Try computing these in
3.0 / 2
3 / 2.0
4 ** 2.0
4.1 % 2
Is the result a float or an integer?
Methods are a way of “doing something with an object”. E.g. in Ruby, numbers have two methods that allow you to check whether the number is odd or even.
Look through the documentation for integer
Fixnum) and find the methods that tell if a number is odd or
irb, use these methods to find out if certain numbers are odd or even.
0, 1, 2, 99, -502 etc.
You can use a method by appending a dot
. and then the method name
to the object. E.g.
-99.abs uses (we also say: “calls”) the method
abs on the number
Try for yourself what it does, and google for “ruby abs” to find the documentation for this method.