Ruby Data Types
When we think of 'data' we might think of a spreadsheet and "crunching the numbers." However, data is made up of more than just numbers. Programming languages work many similar data types, sometimes with different names. The follow types are native to Ruby 2.4.1. (Note: Ruby 2.4.1 has deprecated the use of "Fixnum" and "Bignum". Every number is now either an integer or float and inherits from the Numeric class)
- Strings --------------------- = "abc"
- Symbols ------------------- = :abc
- Integers -------------------- = 5
- Floating Point ----------- = 3.14159
- Boolean -------------------- = true or false
- Range ----------------------- = 1..10
- Array ------------------------- = ['Ally', 'Bob', 'Sarah']
- Hash ------------------------- = { 'name' : 'Ally', 'address' : '123 Yellow Brick rd' }
- Nil ---------------------------- = Null, Nothing, No value
These data types are essential to constructing programs with Ruby. They are also commonly found other programming languages. If you decide to learn another language, the concepts you learn here will be transferable. Next, we'll be examining each data type individually, starting with strings.
String
The string is any sequence of standard characters encased in quotation marks. Ruby is not strict with enforcing single or double quotes for strings, either will work. There are more Ruby methods that respond to double quoted strings. Therefore, as a best practice, it's best to err on the side of caution and use double quoted strings. One example of this is string interpolation.
String Interpolation
In Ruby, variables can be interpolated with the following syntax, #{ }
.If you think of a variable as a box, by interpolation you can unwrap the variable and reveal the value. String interpolation is a process found in most programming languages. For example, in Swift, Apples programming language, the syntax for string interpolation is \( )
. In Perl, PHP, and JavaScript the syntax is $ { }
. (Note: In each example, the variable should be put inside of the parenthesis or brackets.) Programming languages share common sets of features and processes, though the syntax between them may vary. Some examples of strings are, "abc", "John Doe", "abc123", and "2".
Symbols
Symbols are often a thorn in the side of many beginners. They can be conceptually difficult to grasp. Symbols are easily recognizable as they are prefixed with a ' : ' character. They are scalar value objects used as identifiers, mapping immutable strings to fixed internal values. There are two chief characteristics of symbols, immutability, and uniqueness. Unlike strings, symbols cannot be changed. Their uniqueness allows developers to assign and reference a variable throughout the code, each time referring to the same object id. For now, it's important to recognize that symbols are an efficient way for ruby to allocate memory and they allow us to create unique, unchangeable pointers to objects in our application.
Integers
Integers are countable numbers, their negative equivalents, and zero. 1, 2, -4, 500, and 0 are all integers.
But wait, didn't you say that "2" was a string in an earlier section?
Excellent question. Yes, "2" is a string. Remember that a string is an arbitrary sequence of characters encased in quotations. This could be letters, numbers, words, sentences, etc. So, if we use the number 2, it's an integer. If we use the number within quotes, as in, "2", we're dealing with the string data type. It's important to realize that integers do not include fractional numbers, sometimes called decimals. These numbers have their own class called "Floating Point".
Floating point
Floating point numbers, floats for short, are decimal numbers. Some examples include, 3.4, 10.0, and 24.99. Most programming languages have a special way of dealing with decimal numbers. It is common to have two separate classes for dealing with numbers, Ruby uses integers and floats. What does this mean for developers? Let's use Ruby's built-in methods for working with numbers as a calculator. Check out the IRB session below:
>> 5 * 5
25
>> 20 / 2
10
>> 10 / 3
3
It seems to be working just fine for the first two operations but what happen to the third operation? Ten divided by three incorrectly returns the value three. This is an example of why data types matter. We gave ruby an integer divided by an integer, therefore the return value is an integer. Ruby does the division and returns the truncated result. It doesn't acknowledge the decimal part of the value, only the whole number. We can tell ruby that we do want precision by making either value a floating point. So, 10.0 / 3 and 10 / 3.0 will both return a value of 3.3333. Keep this in mind when working with decimal numbers.
Boolean
The boolean data type is native to most programming languages. A boolean value is either going to be true
or false
. These values represent the truth values of logic. One way in which languages differ is what constitutes a false value. In some languages, zero or the empty set will return false. However, this is not the case in Ruby. There are two values treated as false in Ruby, false and nil. Everything else will be treated as true. Remember, nil does NOT mean zero. Nil is the equivalent of null in other languages. It means there is no value. There is a subtle difference between "no value" and a value of 0. It's a difference you'll need to commit to memory. Ruby's boolean logic can easily be summed up, everything other than false and nil will return true
.
Range
The range type represents an interval, a set of values with a beginning and an end. Ranges are sequential and can contain all letters or all numbers. For instance, 1..26
will consist of numbers 1 through 26, while "A".."Z"
will consist of capital letters A through Z. When a range is created using two dots ..
it is inclusive of the starting and ending number/letter. When created using three dots ...
the range will include the starting but not the ending number/letter. We can test this in irb:
>> a = 1...9
1..9
>> a.include?(9)
false
>> b = 1..9
1..9
>> b.include?(9)
true
First we assign the range 1...9
to the variable 'a'. Notice that ruby returns the range to us. Remember, ruby will always return a value. This is part of the languages object orientation. In this case, the return value when creating a range is the range itself. Using the built-in method include, we can check which range includes the value of 9.
Array
An array in Ruby is an integer-indexed, ordered collection of objects. Array indexing starts at 0, as it does for C, Java, JavaScript, and most other languages. Arrays contain their objects within brackets '[ ]' and each object in an array is separated by a comma. An example of an array would be ["Apple", Orange", "Pear"]
. This is an array of three strings. We would say that "Apple" is at position 0, "Orange" position 1, and "Pear" position 2. We can view the value stored at a particular index as follows:
>> a = ["time", "will", "tell"]
[
[0] "time",
[1] "will",
[2] "tell"
]
>> a[1]
"will"
Here I've created a new array and assigned it to the variable ' a '. I can then access the value at index 'x' by supplying a[x]
. Since arrays start at index 0, a[1]
returns the second object. Ruby offers a great deal of flexibility in the types of data that can be be stored in an array. Strings, Integers, Floats, Symbols, Booleans, Hashes, and even other Arrays can all be stored as objects in a single array. ( Aside: if you want 'color highlighting' and 'pretty print' in your irb sessions you can add the following to the '.irbrc' file in your home directory:
require "awesome_print"
AwesomePrint.irb!
and make sure you have the "Awesome Print" gem installed)
$ gem install awesome_print
Hashes
Hashes are similar to arrays in that they store collections of objects. However, they store their objects as "key:value pairs". Ruby hashes are known as dictionaries in Python. A dictionary is a good analogy for hashes as they also store data in the key:value pair format. Each word in the dictionary would be equivalent to the "key" and the definition it's "value". Hashes contain their objects within curly braces ' { } ' where each object is separated by a comma and each key/value is separated by a colon. An example of a hash would be {"First Name" : "Bob", "Last Name": "Smith", "Email": "[email protected]", "Address": [ "123 yellow brick road", "Emerald City", "Oz", 12345]}
. Notice the key "Address" has an array as its value. Similar to arrays, Ruby is flexible in the types of data that can be stored as values in a hash.
Nil
Nil is a special value that means "nothing" or "no value". This is often incorrectly confused with 0. As mentioned earlier, there is a subtle difference between zero and no value. It's important to remember Ruby's boolean logic. Anything false or nil will return false
. Everything else will return true
.