Ruby Data Types
In this chapter, we will introduce the basic data types in Ruby.
Ruby supports data types including basic Number, String, Ranges, Symbols, as well as special values like true, false, and nil. Additionally, there are two important data structuresβArray and Hash.
Number Types
1. Integers
There are two types of integers. If within 31 bits (four bytes), it is an instance of Fixnum. If it exceeds that, it is an instance of Bignum.
The integer range is from -230 to 230-1. Integers within this range are objects of the class Fixnum. When the integer value is greater than or equal to 2 to the power of 30 (from -262 to 262-1), it automatically converts to the Bignum type.
You can use an optional leading sign before the integer, an optional base indicator (0 for octal, 0x for hex, 0b for binary), followed by a string of digits. The underscore character is ignored in a number string.
You can get the integer value of an ASCII character or an escape sequence marked with a question mark.
Example
123 1_234 -500 0377 0xff 0b1011 "a".ord ?n 12345678901234567890 a1=0 a2=1_000_000 a3=0xa puts a1,a2 puts a3 =begin This is a comment. Called: Embedded Document Comment Similar to C =end
Floating-Point Numbers
Ruby supports floating-point numbers. These are numbers with decimal points. Floating-point numbers are objects of the class Float and can be any of the following.
Example
123.4 1.0e6 4E20 4e+20 f1=0.0 f2=2.1 f3=1000000.1 puts f3
Arithmetic Operations
Addition, subtraction, multiplication, and division operators: + - * /. The exponentiation operator is **.
The exponent does not have to be an integer, for example:
Example
puts 2**(1/4) # 1 divided by 4 is 0, then 2 to the power of 0 is 1 puts 16**(1/4.0)
String Type
A Ruby string is simply a sequence of 8-bit bytes. They are objects of the class String.
Strings enclosed in double quotes allow substitution and the use of backslash notation. Strings enclosed in single quotes do not allow substitution and only permit the use of the two backslash sequences and '.
Example
puts 'escape using ""'; puts 'That's right';
This will produce the following result:
escape using "" That's right
You can use the sequence #{ expr } to substitute the value of any Ruby expression into a string. Here, expr can be any Ruby expression.
Example
puts "Multiplication: #{24*60*60}";
This will produce the following result:
Multiplication: 86400
Example
name="Ruby"
puts name
puts "#{name+", ok"}"
The output is:
Ruby Ruby, ok
Backslash Notation
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 |
|
Backslash character |
For more details about Ruby strings, please see Ruby Strings.
Arrays
Array literals are defined by a comma-separated list inside [] and support range definitions.
- (1) Arrays are accessed via
[]indexing. - (2) Elements are inserted, deleted, and replaced through assignment operations.
- (3) Arrays are merged and elements are deleted using the
+and-operators, with the collections appearing as new collections. - (4) Elements are appended to the original array using the
<<operator. - (5) Array elements are repeated using the
*operator. - (6) Union and intersection operations are performed using the
|and&operators (note the order).
Example
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, please see Ruby Arrays.
Hash Type
A Ruby hash is a series of key/value pairs placed inside curly braces. Keys and values are separated by a comma and the sequence =>. Trailing commas are ignored.
Example
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, please see Ruby Hashes.
Range Type
A range represents an interval.
A range is represented by setting a starting value and an ending value. Ranges can be constructed using s..e and s...e, or via Range.new.
A range constructed with .. runs from the starting value to the ending value (inclusive of the ending value). A range constructed with ... runs from the starting value to the ending value (exclusive of the ending value). When used as an iterator, a range returns each value in the sequence.
The range (1..5) means it contains the values 1, 2, 3, 4, 5. The range (1...5) means it contains the values 1, 2, 3, 4.
Example
(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, please see Ruby Ranges.
YouTip