Before delving deeper into Ruby, you must first understand some of the data types in Ruby. Understanding these data types will help you to understand what we are going to study next and will also help you understand most of the bugs that will appear in the future.
Data Types
You know that 1
is number and Hi
is letter. You can group most of the character in 2 groups, word and number. Well, in computer science, these groups (word and numbers) are called data types. Different data types represent different data. 1 (one) as a word is different from 1 as a number.
Just like us, computers also recognize several types of data. In the Ruby programming language, there are various types of data. Different data types are used for different purposes. The data types in Ruby include Numbers
, Booleans
, Strings
, Hashes
, Arrays
, and Symbols
. These data types let the computer know what kind of operations it should perform on data.
Numbers
As the name suggests, this data type contains numbers. 1 is data in Numbers
type, 2 is type Numbers
type, and so on. The Numbers data type consists of Integers and Floating-points. 2
is Integers, but 2.0
is floating-point.
Numbers can be processed with certain operations such as addition, subtraction, multiplication, division, and so on.
String
String is a term for "word" or "sentence" in computer science. Actually strings are not limited to words or sentences. Basically, anything you can say can belong to the string data type. Anything delimited by quotes is classified as a string data type. "hey, this is a string"
is a string, "test"
is also a string. "Hello"
is also a string, even "2"
is also a string.
There is another difference between Numbers and Strings. The Numbers data type can perform addition operations, while the string data types can only perform
concatenate operation. This means 1+1=2
, but "1" + "1" = "11"
. I will explain this further later.
Boolean
The boolean data type only consists of 2 data, which are declared as true
or false
. Suppose you want to know if A is greater than B. If A is greater than B, the boolean value is true. If A is less than B, the boolean value is false. Example :
2>3 #this expression return "false"
5<9 #this expression return "false"
Array
Basically, an array is just a collection of data. You probably learned about matrices in high school. Well, you could say the array is the same as the matrix. The difference is, if the matrix contains only numbers (Numbers), the array can contain numbers (Numbers) and words/sentences (strings). For example, \(\left[\begin{array}{cc}1&2\end{array}\right]\) matrix can be expressed as [1,2]
array, and \(\left[\begin{array}{cc}1&2\\3&4\end{array}\right] \) array can be expressed as [[1,2],[3,4]]
matrix. Yes, that's it.
Hash
The hash data type is similar to the array data type. The difference is, in the hash data type we map a key to a value. Because of this mapping concept, the hash data type is also called the map data type.
Hard enough to imagine? The concept of a hash data type is similar to that of a dictionary. Let's take an analogy to the English - Indonesian dictionary. Key in the dictionary is in English, while the value in the dictionary is in Indonesian. So if you want to find the translation of a word in the dictionary, you look for the English word (key) first. If you find it, then you can see the translation of that word (value). For example { 'word'=>'kata', 'book'=>'buku'} . By looking at 'book', you can see the translation, which is 'buku'. Well, the hash data type works exactly like that. That's why in other programming languages (such as python) this hash type is also called a dictionary data type. Here is the syntax of the hash data type:
data = {"name" => "ruby", "creator" => "matz", "country" => "japan"}
In simple terms, the above code states that "ruby"
is the value of "name"
, "Matz"
is the value of "creator"
, and so on.
Actually, there is another data type in ruby, but i think we can talk about it later.