Ruby Data Types
Understanding different types of data in Ruby
💎 What are Ruby Data Types?
Ruby data types represent different kinds of values you can work with. Ruby is dynamically typed, meaning you don't need to declare variable types. Common types include numbers, strings, booleans, arrays, and hashes.
# Ruby automatically determines data types
name = "Alice" # String
age = 25 # Integer
price = 19.99 # Float
is_student = true # Boolean
Common Ruby Data Types
Numbers
Integer and Float values
count = 42
price = 9.99
Strings
Text and character data
name = "Ruby"
message = 'Hello'
Booleans
True or false values
is_active = true
is_closed = false
Symbols
Immutable identifiers
status = :active
role = :admin
🔹 Integer Data Type
Integers are whole numbers without decimal points. Ruby handles both small and large integers automatically.
# Integer examples
age = 25
year = 2024
negative = -10
large_number = 1_000_000 # Underscores for readability
puts age # Output: 25
puts age.class # Output: Integer
Output:
25 Integer
🔹 Float Data Type
Floats are numbers with decimal points. They're used for precise calculations and measurements.
# Float examples
price = 19.99
temperature = -5.5
pi = 3.14159
puts price # Output: 19.99
puts price.class # Output: Float
puts temperature # Output: -5.5
Output:
19.99 Float -5.5
🔹 String Data Type
Strings store text data. You can use single or double quotes, but double quotes allow interpolation.
# String examples
name = "Alice"
greeting = 'Hello'
message = "Welcome, #{name}!" # String interpolation
puts name # Output: Alice
puts message # Output: Welcome, Alice!
puts name.class # Output: String
Output:
Alice Welcome, Alice! String
🔹 Boolean Data Type
Booleans represent true or false values. They're essential for conditional logic and decision-making in programs.
# Boolean examples
is_student = true
is_admin = false
puts is_student # Output: true
puts is_student.class # Output: TrueClass
puts is_admin # Output: false
puts is_admin.class # Output: FalseClass
Output:
true TrueClass false FalseClass
🔹 Symbol Data Type
Symbols are immutable identifiers that start with a colon. They're more memory-efficient than strings for repeated values and commonly used as hash keys.
# Symbol examples
status = :active
role = :admin
color = :red
puts status # Output: active
puts status.class # Output: Symbol
puts :active.object_id == :active.object_id # Output: true
Output:
active Symbol true
🔹 Nil Data Type
Nil represents the absence of a value. It's Ruby's way of expressing "nothing" or "no value".
# Nil examples
empty_value = nil
puts empty_value # Output: (blank)
puts empty_value.class # Output: NilClass
puts empty_value.nil? # Output: true
Output:
NilClass true
🔹 Checking Data Types
Ruby provides methods to check and convert between data types easily.
# Type checking and conversion
value = 42
puts value.class # Output: Integer
puts value.is_a?(Integer) # Output: true
puts value.to_s # Output: "42" (convert to string)
puts value.to_f # Output: 42.0 (convert to float)
text = "100"
puts text.to_i # Output: 100 (convert to integer)
Output:
Integer true 42 42.0 100
💡 Key Points to Remember:
- Ruby is dynamically typed - no need to declare types
-
Use
.classto check a variable's data type - Integers and Floats are both numeric types
- Symbols are immutable and memory-efficient
- Nil represents the absence of a value
-
Use conversion methods like
.to_i,.to_s,.to_f