HexLibraryHexLibrary
</> HexCodeSign inSign up
PythonJavaScriptTypeScriptCC++JavaPHPGoRustRubyBashSQLLuaPerlCOBOLPascalDelphiFortranDartSwiftC#JuliaGroovyRElixirHaskellKotlinScalaClassic ASPClipperMS-DOS
LESSONS
Introduction to RubyVariablesData TypesOperatorsStringsArraysHash (Dictionary)Conditions (if / else)LoopsMethods (def)Classes
REFERENCE
📖 Overview
🖥️ Output & Input
📦 Variables
🔢 Numbers & Math
Arithmetic ( + - * / % ** )round / floor / ceil / absMath.sqrt / rand
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›REFERENCE›Numbers & Math
🔢

Numbers & Math

Arithmetic and mathematical methods.

CommandDescription
Arithmetic ( + - * / % ** )Four basic operations, modulo, and exponentiation.
round / floor / ceil / absRounding and absolute value.
Math.sqrt / randSquare root and random number.

Arithmetic ( + - * / % ** )

Try it yourself →

Four basic operations, modulo, and exponentiation.

Syntax

a + b, a % b, a ** b

Description

Standard operators. ** is exponent, % is modulo. Integer division returns an integer: 7 / 2 = 3.

Example

puts 7 % 2
puts 2 ** 10
puts 7 / 2
puts 7.0 / 2

Expected output

1
1024
3
3.5

When to use?

Calculations; watch out for /: decimals are dropped in integer division.

Common mistakes

  • ⚠️ 7 / 2 → 3 (integer division). For decimals, write 7.0 / 2.

round / floor / ceil / abs

Try it yourself →

Rounding and absolute value.

Syntax

number.round(digits)

Description

round rounds to the nearest, floor rounds down, ceil rounds up; abs returns the unsigned value.

Example

puts 3.14159.round(2)
puts 4.9.floor
puts(-7.abs)

Expected output

3.14
4
7

When to use?

Displaying money, averages.

Math.sqrt / rand

Try it yourself →

Square root and random number.

Syntax

Math.sqrt(x) · rand(n)

Description

Math.sqrt returns the square root. rand(n) generates a random number between 0..n-1, rand(1..6) within a range.

Example

puts Math.sqrt(144)
zar = rand(1..6)
puts (zar >= 1 && zar <= 6)

Expected output

12.0
true

When to use?

Geometry, dice, random selection.

HexLibraryHexLibrary

Learn programming more clearly. Every command is explained line by line with colors, reinforced with runnable exercises, test your code instantly — all in one place, free.

Learn

  • Home
  • Languages & Lessons
  • HexCode

Company

  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

Other Projects

  • HexToolz
  • Free Service Manuals
© 2026 HexLibrary · The clearest way to learn code