Step 5: Booleans
Another basic data type in Ruby is the boolean. Booleans are actually the most basic type in programming, and correspond to the concepts of
To demonstrate the concept of the boolean type, paste the following into repl.it and hit "run".
1 == 1
On the right-hand side of the REPL, you can see that this expression evaluates to
true. The expression is asking the computer if the number
1 is equal to the number
1, which is obviously true.
Here we've also introduced the
== operator. In Ruby,
== is a comparison operator that tests to see if the values on the left and right of the operator are equal, and returns
true if they are and
false if they aren't.
Now try this:
1 == 2
This expression evaluates to false, because as we know
2 are different numbers. Finally, try the following. What do you think it will return?
1 == "1"
This expression evaluates to false because the number
1 and the string
"1" are two different things.
false == false # => true true == false # => false true == "true" # => false
Pay attention to the last expression above, and note again that just like with strings and numbers, the string
"true" and the boolean
true are not equivalent.