Ruby Data Types
In this section, we will introduce the basic data types in Ruby.
Ruby supports data types including basic Number, String, Ranges, Symbols, and special values such as true, false, and nil. It also includes two important data structures: Array and Hash.
Numeric Types (Number)
1. Integer
Integers come in two types: within 31 bits (four bytes), they are instances of Fixnum. If they exceed this, they become instances of Bignum.
The range of integers extends from -2Fixnum to 2Fixnum. When the integer value is greater than or equal to 2^30, it becomes a Bignum.
You can prefix an integer with an optional leading sign, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a string of digits. Underscore characters in the number string are ignored.
You can obtain the integer value of an ASCII character or an escape sequence marked with a question mark.
Example
123 # Fixnum decimal
1_234 # Fixnum decimal with underscore
-500 # Negative Fixnum
0377 # Octal
0xff # Hexadecimal
0b1011 # Binary
"a".ord # Character code of "a"
?\n # Character code of newline (0x0a)
12345678901234567890 # Large number
# Integer literals
a1 = 0
# Integer with thousands separator
a2 = 1_000_000
# Representation in other bases
a3 = 0xa
puts a1, a2
puts a3
# puts and print both print to the console, but puts includes a newline
=begin
This is a comment, called an embedded document comment
Similar to C#'s /**/
=end
Floating Point
Ruby supports floating-point numbers, which are numbers with decimal points. Floating-point numbers are instances of the Float class and can be any of the following:
Example
123.4 # Floating-point value
1.0e6 # Scientific notation
4E20 # Exponent is not mandatory
4e+20 # Sign before the exponent
# Floating-point types
f1 = 0.0
f2 = 2.1
f3 = 1000000.1
puts f3
Arithmetic Operations
Addition, subtraction, multiplication, and division operators are +, -, , /. The exponentiation operator is *.
The exponent does not have to be an integer, for example:
Example
# Exponentiation arithmetic
puts 2**(1/4) # 1 divided by 4 is 0, so 2^0 is 1
puts 16**(1/4.0) # 1 divided by 4.0 is 0.25, so the fourth root of 16 is taken
String Types
Ruby strings are simply sequences of 8-bit bytes and are instances of the String class.
Strings enclosed in double quotes allow substitution and the use of backslash notation, while single-quoted strings do not allow substitution and only allow the use of \ and \'.
Example
#!/usr/bin/ruby -w
puts 'escape using "\\"';
puts 'That\'s right';
This will produce the following result:
escape using "\"
That's right
You can substitute the value of any Ruby expression into a string using the sequence #{ expr }. Here, expr can be any Ruby expression.
Example
#!/usr/bin/ruby -w
puts "Multiplication : #{24*60*60}";
This will produce the following result:
Multiplication : 86400
Example
#!/usr/bin/ruby -w
name = "Ruby"
puts name
puts "#{name+",ok"}"
Output:
Ruby
Ruby,ok
Backslash Notations
The following table lists the backslash notations supported by Ruby:
Notation | Character Represented | |
---|---|---|
\n | Newline (0x0a) | |
\r | Carriage return (0x0d) | |
\f | Formfeed (0x0c) | |
\b | Backspace (0x08) | |
\a | Bell (0x07) | |
\e | Escape (0x1b) | |
\s | Space (0x20) | |
\nnn | Octal notation (n is 0-7) | |
\xnn | Hexadecimal notation (n is 0-9, a-f, or A-F) | |
\cx, \C-x | Control-x | |
\M-x | Meta-x (c | 0x80) |
\M-\C-x | Meta-Control-x | |
\x | Character x |
For more details about Ruby strings, see Ruby Strings.
Arrays
Array literals are defined within [] with elements separated by commas and support range definitions.
- Arrays are accessed by indexing with []
- Elements can be inserted, deleted, or replaced through assignment operations
- Arrays can be merged or elements removed using + and -, with the result as a new array
- Elements can be appended to the original array using <<
- Arrays can be repeated using *
- Union and intersection operations can be performed using | and &
Example
#!/usr/bin/ruby
ary = [ "fred", 10, 3.14, "This is a string", "last element", ]
ary.each do |i|
puts i
end
This will produce the following result:
fred
10
3.14
This is a string
last element
For more details about Ruby arrays, see Ruby Arrays.
Hash Types
Ruby hashes are collections of key/value pairs enclosed in curly braces, with keys and values separated by commas and the => operator. Trailing commas are ignored.
Example
#!/usr/bin/ruby
hsh = colors = { "red" => 0xf00, "green" => 0x0f0, "blue" => 0x00f }
hsh.each do |key, value|
print key, " is ", value, "\n"
end
This will produce the following result:
red is 3840
green is 240
blue is 15
For more details about Ruby hashes, see Ruby Hashes.
Range Types
A range represents an interval.
A range constructed with .. runs from the start value to the end value (inclusive). A range constructed with ... runs from the start value to the end value (exclusive). When used as an iterator, a range returns each value in the sequence.
The range (1..5) includes the values 1, 2, 3, 4, 5, while the range (1...5) includes the values 1, 2, 3, 4.
Example
#!/usr/bin/ruby
(10..15).each do |n|
print n, ' '
end
This will produce the following result:
10 11 12 13 14 15
For more details about Ruby ranges, see Ruby Ranges.