Skip to main content

Ruby Datatypes

For the purpose of the exercises, the datatypes discussed herein are String, Numeric, Boolean, Array, and Hash.

String

A String type implies a string of characters. This datatype may more commonly be recognized as words, sentences, or text. The most common ways to initialize a string in Ruby is by enclosing a string of characters in single-quotation marks ('') or double-quotation marks ("").

STRINGS
'this is a string'
"this is also a string"

Numeric

There are several numeric datatypes within Ruby. The only one necessary to cover for this material in any depth is the Integer type, though others exist for handling cases of decimal positions and VERY BIG numbers.

NUMERICS
0
1
345

Boolean

The boolean datatype registers a value of either True or False. This is useful for making logical decisions within a programming script.

BOOLEANS
true
false

Array

The Array datatype captures what is more commonly referred to as lists of elements. An array may contain zero or more member elements. The position of an element in array is called its index. Indexes are integers. In Ruby, as is typical in most scripting languages, arrays are zero-indexed meaning the first index is referred to as the 0^th^ index, and the numbers increment by 1 from there.

Arrays have a few methods of representation within Ruby; the convention within this material is to initialize arrays using the square bracket notation, [].

ARRAYS
[]
['string1', 'string2', 'string3']
[100, 101, 102, 103]

To access the value found at a given index of an array, one may use the array accessor, [], as is common in most C-derived software languages. To test array accessing, see this simple software program where the third value (index 2) is accessed and printed as output. If you are following along, be sure to invoke the script from a command-line interface.

COMMAND LINE
puts ['a', 'b', 'c'][2]
OUTPUT
c

Hash

The Hash datatype stores data in key-value pairs. In use, it is similar to the Array type with one primary difference. Where, in an array, values are accessed using numeric indexes, Hash keys can be any datatype, and it is up to the programmer to name the keys. This is a very useful datatype for creating maps/dictionaries such that related data-values can be stored in one data object, while still being able to be referenced by a naming structure sensible to a software writer/reader.

Hash representation starts with curly brace initialization. While keys within a hash may be any datatype, this manual will use the convention of using Symbols as keys. Values maybe any datatype or created object.

HASHES
{ a: 1, b: 2, c: 3 }
{ age: 24, height: "6'2" }
{
sally: { age: 24, height: "6'2" },
joann: { age: 42, height: "5'6" }
}

To access the value found at a given key of an array, one may use a square bracket accessor, [], just like array. Instead of an index, the characters within the bracket should be a key in the object. To test accessing a hash value, see this simple software program where the value at key :orange is accessed and printed as output. If you are following along, be sure to invoke the script from a command-line interface.

COMMAND LINE
puts { orange: 'juicy' }[:orange]
OUTPUT
juicy

Ruby Objects

While the term "Datatype" has been used throughout this manual, the Ruby programming language defines all instances of data as an Object. The string 'string' is a String object. The number 123 is an Integer object. The Booleans true and false are Boolean objects.

This attribute of the Ruby language becomes powerful when it is recognized that new Object types can be defined and instantiated by the software writer.