What is Math? (For Rubyists)

Blake Runyon
3 min readMar 1, 2021

In Ruby we call Math:

“The Math module contains module functions for basic trigonometric and transcendental functions.”

Ahh transcendental functions and basic trigonometrics, those things we’ve all come to love over the years. Let’s break this down for those of us who have less than a 0% understanding of what that sentence means.

After some googling I found the following definitions:

Transcendental Functions: This area of math deals with equations that can’t be expressed in algebra; exponential function, logarithm, and trigonometric functions.

Trigonometric Functions: These functions are the area of math dealing with triangles; sine, cosine, tangent, etc.

Ok… cool.

So again, our definition is:

“The Math module contains module functions for basic trigonometric and transcendental functions.”

Well, what is a module?

A module is very similar to a class in Ruby. The only difference is, you can’t actually instantiate a new instance of a module. In other words, you can’t call Math.new(…)

The power of modules is their ability to group functions together, and include them in your class.

So check this code out:

module Math_test  PI = 3.14  def acos num
Math::PI/2
end
end
class Object
include Math_test
end
new_Object = Object.newnew_Object.acos 12 => 1.57Object::PI => 3.14

Let’s go through a few lines of note:

This module is just a place where we can group all our math functions.

module Math_test

This is a constant in our module. Constants are just variables that can not be changed. They’re declared with all capital letters.

PI = 3.14

Here we have a module function that we can access later.

def acos num
Math::PI/2
end

Now we’re declaring a class, and including our module (which will give us all those functions and constants we created.

class Object
include Math_test
end

Finally, we can create a new instance of our class. Then call our new methods on it!

new_Object = Object.newnew_Object.acos 12 => 1.57Object::PI => 3.14

Simple :D

So how does this work in Ruby at scale? Well for our TL:DR

  1. Ruby has a BasicObject class at the very top of it’s structure.
  2. A child of the BasicObject class is our Object class.
  3. Now included in this Object class is the module called Kernel.
  4. The Kernel gives the Object class access to all of our typical Ruby stuff: Array, String, Float, Enumerables, etc.
  5. The Kernel is also where the Math module is included!
  6. But keep in mind, Math is it’s own module, and it’s included in the Kernel.

That means when we’re writing the majority of our Ruby code, we’re working within the the Object class, and using the Kernel module.

Then when we call Math::PI, we’re accessing our Object Class, which is using the Kernel Module, which is using the Math Module…

The reason for all this nesting? It’s so those of use who aren’t yet well-grounded rubyists, don’t mess up our code in an irreversible way.

Well my brain is broken now. So to sum it all up.

  1. Math is module in Ruby code that gives us access to a few constants and several high level math methods.
  2. Our two constants are: PI & E
  3. Our methods can be categorized as Trig and Calculus focused.
  4. Ruby comes with a lot of class inheritance and methods fresh out of the box.

--

--