0Pricing
Ruby Academy · Lesson

The Math Module

sqrt, sin, log.

The Math Module is a free Ruby Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

The Math Module

The Math module provides advanced math functions and constants. You call its methods with the module name, like Math.sqrt.

puts Math.sqrt(16)

Square Root

Math.sqrt returns the square root as a float.

puts Math.sqrt(2)
puts Math.sqrt(144)

Cube Root and Powers

Math.cbrt gives the cube root. For powers you can also use Math.pow via ** or Math.exp.

puts Math.cbrt(27)
puts 2 ** 0.5

Constants PI and E

The module exposes Math::PI and Math::E as constants.

puts Math::PI
puts Math::E

Trigonometry

sin, cos, and tan work in radians. Multiply degrees by PI/180 to convert.

puts Math.sin(Math::PI / 2)
puts Math.cos(0)

Logarithms

Math.log is the natural log. Math.log10 and Math.log2 use those bases.

puts Math.log(Math::E)
puts Math.log10(1000)
puts Math.log2(8)

Exponential

Math.exp(x) computes e raised to the power x, the inverse of natural log.

puts Math.exp(0)
puts Math.exp(1).round(4)

Hypotenuse

Math.hypot computes the hypotenuse, the square root of a squared plus b squared.

puts Math.hypot(3, 4)

Distance Example

Combine functions to compute the distance between two points.

x1, y1 = 0, 0
x2, y2 = 3, 4
d = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2)
puts d

Degrees to Radians

Build a small helper to convert degrees so trig functions read naturally.

def radians(deg)
  deg * Math::PI / 180
end
puts Math.sin(radians(90))

Circle Area

Use PI to compute the area of a circle.

radius = 5
area = Math::PI * radius ** 2
puts area.round(2)

Quick Check

Check your Math module knowledge.

Recap

You explored the Math module:

  • sqrt, cbrt, hypot
  • constants PI and E
  • trig: sin, cos, tan in radians
  • logs: log, log10, log2, and exp

Next: exact arithmetic with Rational and BigDecimal.

puts Math.hypot(5, 12)

Frequently asked questions

Is the “The Math Module” lesson free?

Yes — the full text of “The Math Module” is free to read here on the web, and the Ruby Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Ruby Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Math Module”?

sqrt, sin, log. You practise Ruby Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Ruby Academy?

No prior experience is required. Ruby Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “The Math Module” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Ruby Academy lesson?

Yes. Every Ruby Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Integers and Floats
  2. Arithmetic and Operators
  3. The Math Module
  4. Rational and BigDecimal
← Back to Ruby Academy