Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic 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

0:00
The math module is really interesting.

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

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

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

0:14
operate on an instance of a class.

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

0:22
We've seen methods before, and

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

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

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

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

0:42
Okay.

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

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

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

0:55
for basic trigonometric and transcendental functions.

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

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

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

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

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

1:22
Now to access the math constant,

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

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

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

1:39
The math library also will return Pi.

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

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

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

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

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

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

2:07
Returns the nonnegative square root of x.

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

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

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

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

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

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

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

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

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

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

2:58
And it returns 0.54.

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

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

3:13
the hypotenuse of a rightangle triangle, with the sides x and y.

3:19
So if we try that.

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

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

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

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

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

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

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

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

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

3:58
Using the documentation in the teacher notes,

4:01
try out some of the different math methods on your own now, using workspaces.
You need to sign up for Treehouse in order to download course files.
Sign up