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
✍️ String
📋 Array
🗂️ Hash (Dictionary)
🔁 Condition & Loop
🧩 Method & Class
Ruby›Data Types

Data Types

String, Integer, Float, Boolean; type checking with .class and conversion with to_i/to_s.

Simple enough for a 5-year-old to understand

Basic types and .class

Example code
1
puts "Alex".class
String
Output / InputString
2
puts 36.class
Integer
Output / InputInteger
3
puts 1.72.class
Float
Output / InputFloat
4
puts true.class
TrueClass
Output / InputTrueClass
Output / Input
💡 Hint: Everything is an object in Ruby; you can find its type using .class.

About this topic

In Ruby, every value is an object and every object has a class. Integers belong to the Integer class, decimals to Float, and texts to String. The practical result of this consistency is: you can even call methods on a number.

Ruby AUTOMATICALLY switches to large numbers for integers. There is no limit; even if a factorial calculation reaches a hundred digits, there is no overflow. This is an important difference for those coming from languages like C and Java.

A behavior to watch out for is integer division: the division of two integers is an integer and the decimal part is discarded. Ruby is the same as C in this regard and gives no warning; it silently produces an incorrect result.

What you will learn

  • That basic classes and every value is an object
  • The result of integer division
  • Type conversion methods
  • That everything except nil and false is considered true
  • What symbols are

When to use?

Float is not used in money calculations; decimals cannot be represented exactly in binary and rounding errors accumulate. In Ruby, rational numbers or the decimal class are used for this task.

Symbols are used for fixed labels and key names: each symbol is created only once in memory, so it is faster and takes up less space than text.

Common mistakes

Wrong

puts 7 / 2

The division of two integers is an integer: 3 is printed to the screen, not 3.5. The decimal part is discarded without rounding and no warning is given. For a decimal result, at least one of the operands must be a decimal or the conversion method must be called.

Correct

puts 7.0 / 2
puts 7.fdiv(2)

Wrong

age = "25"
puts age + 5

Text cannot be added to a number; the interpreter gives the error "no implicit conversion of Integer into String". Ruby does not do silent conversions between types and this is a good thing: unexpected concatenations like in JavaScript do not occur. You need to write the conversion explicitly.

Correct

age = "25"
puts age.to_i + 5

Wrong

value = 0
if value
  puts "Zero is considered false"
end

Zero is TRUE in Ruby; the only things considered false are nil and false. That's why the block runs. This is the most common trap for those coming from C, Python, and JavaScript. If you want to check for zero, you must write the comparison explicitly.

Correct

value = 0
if value.zero?
  puts "Value is zero"
end

Frequently asked questions

What is the difference between a symbol and a string?

A symbol is created only once in memory and cannot be changed; even if you write the same symbol a thousand times, there is only one object. Text, on the other hand, creates a new object every time it is written. Therefore, symbols are preferred for fixed names like keys and labels.

Is there a limit on integers?

No. Ruby automatically switches to large number representation when the value grows; there is no overflow. The cost is speed — operations with large numbers are slow — but correctness is guaranteed, which is the right trade-off in most cases.

How do I find out the type of a value?

The class method returns the class of the value. However, the usual approach in Ruby is not to ask for the type, but to ask if the method exists: using respond_to? is more flexible because it works with different classes that offer the same behavior.

Summary

Every value is an object and methods can be called.

Integer division silently discards the decimal part.

There is no implicit conversion between types; it must be written explicitly.

Every value except nil and false is counted as true — including zero.

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