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
puts "Alex".classputs 36.classputs 1.72.classputs true.classString, Integer, Float, Boolean; type checking with .class and conversion with to_i/to_s.
Simple enough for a 5-year-old to understand
puts "Alex".classputs 36.classputs 1.72.classputs true.classIn 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.
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.
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
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.
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.
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.
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.