This is the first in a new series on beginning development with Ruby. Each entry in the series will cover a different basic programming concept starting with data types. All modern programming languages have a defined set of data types, most of which are shared between languages. Part of what sets Ruby apart from .Net languages like C# and VB.Net is that all data types are objects.
The classes for these data types are:
I want you to remember back to when you were in elementary school passing the pretty girl a note asking her if she like likes you, check yes or no. Boolean values represent that yes or no. It is a flag that hold either true or false. Even though it holds true or false, you see boolean values everywhere. Anyplace you see only two options, true or false, yes or no, black or white, up or down, it can be represented by a boolean.
The two most commonly used types of numbers are integers and floats.
Integers are whole numbers, or numbers that do not have a decimal place or fraction. Integers can be either negative or positive numbers. So when someone asks you what the square root of nine is, you can represent both answers with an integer, 3 and -3. I still remember that number chart above the chalk board in school that was used to teach how to add and multiply negative and positive numbers together.
Floats are the equivalent to Decimals in .Net and hold numbers with a decimal place. They are used to represent money like the price of gas that never seems to be a whole penny value, and evil fractions in their decimal form, 2 3/4 would be represented by 2.75. Dividing an integer and float will provide a float value.
Strings are one or more characters of letters, numbers, and punctuation. Most of what is presented to the user is a collection of strings with other data types sprinkled in here and there. These characters are enclosed by either single or double quotes. Double quotes allow for string interpolation and escaped characters, characters with the back slash before it and is used to represent special meaning or action, like \n for newline. Single quotes will keep the special characters and print them with the other characters in the string.
= Hello \nBacon
For more information on strings, check out the Ruby Documentation
An array is a collection of values of any type, unlike languages like C and C# where the values in the array have to all be of the same type. Values are stored and retrieved by their index, which is a zero based index. A zero based index starts at zero instead of one and increments from there and an index of 9 would be the 10th value in an array. All values have a unique index. You can think of this as a key value collection by thinking of the array index as the key and the value you add to the array as the value.
For more information on arrays, check out the Ruby Documentation.
Similar to arrays, hashes store a collection of values in a key and value fashion. Unlike arrays, hashes do not assign an index for the value you add, but instead uses a value you provide as the key.
Array[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:
Key = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
Value = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Hash["FirstName" => "Mark", "LastName" => "Brown", "Status" => "Bacon"]:
Key = “FirstName”, “LastName”, “Status”
Value = “Mark”, “Brown”, “Bacon”
For more information on hashes, check out the documentation at Ruby Documentation.
The best description I have found for Symbols is the one given in Why’s (Poignant) Guide. In there it is stated that symbols are light weight strings that are generally used when the string value will not be printed. I have to admit I am not 100% sure of what these are. Are they pointers or the equivalent of references in .Net? Or are they a class, like String, but with limited functionality, like a beer with half the calories? Feel free to leave a comment if you would like to help straighten this out for me.
For more information on symbols, check out the documentation at Ruby Documentation.