Data Types in Ruby
Ruby is an Object-Oriented Language. Ruby can represent different types of data such as strings, numbers, and text. A full list of the different data types in Ruby are as follows:
1. Numbers
2. Symbols
3. Boolean
4. Arrays
5. Strings
6. Hashes
Numbers
Numbers can be defined as series of digits, using a dot as decimal notation. Ruby recognizes both integers and floating point numbers.
# Addition of two integers
puts 2 + 3# Addition of integer and float
puts 2 + 3.0# Subtraction of two integers
puts 5–3# Multiplication and division of two integers
puts 2 * 3
puts 6 / 2# Exponential operation
puts 2 ** 3
Boolean
A Boolean is a data type that represents information as either true or false.
Strings
A string can be defined as a group of letters that represent a word or a sentence. You designate a string by enclosing the text with either single ‘ ’ or double “ ” quotes.
'This is a string in single quotes.'
"This is a string in double quotes."
Hashes
Hashes are another data type that will assign values to the key using the => sign. A key value pair is always enclosed within curly brackets. Similary in JavaScript, hash is very similar to an object.
Arrays
An array can store data or a list of data and can include all different types of data. Data an array can be separayed by a comma and are enclosed within square brackets. Always remembers, the first position (index) in an array starts at 0.
ary = [1, "two", 3.0] #=> [1, "two", 3.0]
Symbols
Symbols are basically light-weight strings. To use a symbols, precede the text with a colon. Sometimes, symbols are used instead of strings because they take up much less memory.
:rubysymbol