**Heads up!** To view this whole video, sign in with your Courses account or enroll in your free 7-day trial.
Sign In
Enroll

Preview

Start a free Courses trial

to watch this video

In this video, we're going to look at the Math module in Ruby. As you might expect based on the name, it provides math functionality such as sins, logs, tangents, and more. However, none of these methods really really make sense to operate on an instance of a class. In this case, we interact with the Math module using class methods.

#### Links

#### Code Samples

```
puts Math::E # => 2.718281828459045
puts Math::PI # => 3.141592653589793
puts Math.sqrt(9) # => 3.0
puts Math.cos(1) # => 0.5403023058681398
puts Math.hypot(2, 2) # => 2.8284271247461903
puts Math.log(2, 10) # => 0.30102999566398114
puts Math.log(2, 12) # => 0.2789429456511298
```

The math module is really interesting.
0:00

As you might expect based on the name,
0:03

it provides math functionality such
as sines, logs, tangents and more.
0:05

However none of these
methods really makes sense to
0:11

operate on an instance of a class.
0:14

In this case, we interact with
the math module using class methods.
0:17

We've seen methods before, and
0:22

class methods are written
the same way as instance methods.
0:24

The big difference is that
class methods aren't tied to
0:28

any particular instance of a class,
and wouldn't really make sense to.
0:32

Let's take a look at the math
module now using work spaces.
0:37

Okay.
0:42

So now we're looking at
the math module rdoc.
0:43

You can find a link to this in
the teacher notes right below this video.
0:46

So we can see from the description that
the math module contains module functions
0:51

for basic trigonometric and
transcendental functions.
0:55

So here's something
interesting about the rdoc,
1:00

it lists the constants up
at the top right here.
1:03

Now we learned about name spacing and
Ruby constants, so
1:07

let's just go ahead and pop open IRB and
take a look at E and Pi.
1:11

So here I have loaded IRB
inside of a workspace.
1:18

Now to access the math constant,
1:22

we need to use the math module, and
then the constant lookup operator.
1:25

And then the constant that we want,
which in this case is E.
1:31

And it will return
the mathematical constant, E.
1:35

The math library also will return Pi.
1:39

And we access that the same way,
by using the constant lookup operator,
1:43

and then the constant that we want,
which is Pi.
1:47

Now in addition to that, we have all of
these different methods to work with.
1:51

Now we're not going to go over
every single one of these.
1:57

Just maybe some of the more common
ones that you have seen before.
2:00

Let's go ahead and look at square root.
2:03

Returns the non-negative square root of x.
2:07

Let's go ahead and
try that over in work spaces.
2:11

So let's try looking at
the square root of nine.
2:14

Now when we're accessing a class method,
we call it on the class.
2:17

So we can call Math.sqrt,
and then pass in a number,
2:24

which is nine and then it returns
3.0 just like we would expect.
2:30

So class methods are really, really easy.
2:36

They're just called on the class
rather than an instance.
2:38

So, let's see what other things we have
in here that we can take a look at.
2:41

We can take a look at the cosine of X, and
2:46

if we tab back over to our rdoc
we could say Math.cos of 1.
2:51

And it returns 0.54.
2:58

We also have options for
doing the hypotenuse.
3:01

If we look on the left up here,
we get hypot, which is short for
3:08

the hypotenuse of a right-angle triangle,
with the sides x and y.
3:13

So if we try that.
3:19

We'll just put two and two in there.
3:23

We get 2.82 and so on and so forth.
3:25

And this also works with Math.log.
3:29

So try that for two and base ten.
3:32

We can also try the log of two and
base 12.
3:35

Now if you're curious
about any of these or
3:38

don't remember what we've done,
you can go ahead and open up math.rb.
3:42

And, that is all inside
of this math.rb file.
3:49

If you're curious about what that
returns or how to take a look at it.
3:53

Using the documentation
in the teacher notes,
3:58

try out some of the different math methods
on your own now, using workspaces.
4:01

You need to sign up for Treehouse in order to download course files.

Sign up