0Pricing
Ruby Academy · Lesson

Rational and BigDecimal

Exact arithmetic.

Rational and BigDecimal is a free Ruby Academy lesson on CoddyKit — lesson 4 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.

Why Exact Arithmetic?

Floats lose precision. For money and fractions Ruby offers Rational and BigDecimal that stay exact.

puts 0.1 + 0.2

Creating a Rational

A Rational is an exact fraction. Write it with r suffix or Rational(num, den).

puts (1r / 3)
puts Rational(2, 4)

Rationals Auto-Simplify

Rationals reduce to lowest terms automatically, so 2/4 becomes 1/2.

puts Rational(6, 8)
puts Rational(10, 5)

Exact Fraction Math

Adding rationals stays exact, unlike floats. One third plus one third plus one third is exactly one.

third = Rational(1, 3)
puts (third + third + third)

Numerator and Denominator

Read the parts with numerator and denominator.

r = Rational(3, 4)
puts r.numerator
puts r.denominator

Converting Rationals

to_f turns a rational into a float when you need a decimal approximation.

puts Rational(1, 8).to_f

Loading BigDecimal

BigDecimal handles exact decimals, ideal for money. Require it from the standard library first.

require "bigdecimal"
a = BigDecimal("0.1")
b = BigDecimal("0.2")
puts (a + b).to_s("F")

Always Use Strings

Create BigDecimals from strings, not floats, so no precision is lost before it starts.

require "bigdecimal"
good = BigDecimal("1.1")
puts good.to_s("F")

Money Calculation

BigDecimal keeps cents exact across many operations.

require "bigdecimal"
price = BigDecimal("19.99")
qty = 3
total = price * qty
puts total.to_s("F")

Rounding BigDecimal

round on a BigDecimal controls decimal places precisely.

require "bigdecimal"
n = BigDecimal("2.675")
puts n.round(2).to_s("F")

Choosing the Right Tool

Use Rational for exact fractions and ratios. Use BigDecimal for exact decimal money. Use Float for fast scientific work where small error is fine.

require "bigdecimal"
puts Rational(1, 3)
puts BigDecimal("0.33").to_s("F")

Quick Check

Check exact arithmetic.

Recap

You learned exact arithmetic:

  • Rational for exact fractions that auto-simplify
  • numerator, denominator, to_f
  • BigDecimal from strings for exact money math
  • choose the right type for the job

That completes the Ruby Numbers and Math course.

puts (Rational(1, 2) + Rational(1, 3))

Frequently asked questions

Is the “Rational and BigDecimal” lesson free?

Yes — the full text of “Rational and BigDecimal” 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 “Rational and BigDecimal”?

Exact arithmetic. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Rational and BigDecimal” 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