The Math Module
sqrt, sin, log.
The Math Module is a free Ruby Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Ruby Academy learning path, and your progress syncs across the web and the CoddyKit app. The Ruby Academy course includes 4 lessons in total.
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.5Constants PI and E
The module exposes Math::PI and Math::E as constants.
puts Math::PI
puts Math::ETrigonometry
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 dDegrees 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
PIandE - trig:
sin,cos,tanin radians - logs:
log,log10,log2, andexp
Next: exact arithmetic with Rational and BigDecimal.
puts Math.hypot(5, 12)Learn Ruby with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 29
- Lessons
- 109
Frequently Asked Questions
Is the “The Math Module” lesson free?
Yes — any 3 lessons of the Ruby Academy learning path, including “The Math Module”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Ruby Academy course includes 4 lessons in total.
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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.
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.