Everything is regarded as an object in Ruby.
Ruby’s built-in data types are false
and nil
.
|
|
|
|
|
|
|
|
Note: In Ruby,
true
,false
, andnil
refer to objects rather than integers. Thetrue
andfalse
are not the same as 0, andtrue
andnil
are not the same as 1.
When Ruby needs a boolean value, nil
is treated as false
, and values other than nil
or false
are treated as true
.
students = ["Jeery", "Tom", "Eddie"]#this will print student at index 0puts students[0]#nil#this will output nothing since index doesnot existputs students[4]#this will output the class that belongs to nilputs students[4].class#false#this will output false since both names does not matchputs students[0] == students[1]#this will output the class that belongs to falseputs (students[0] == students[1]).class#special case#nil treated as falseJeery = nilif Jeeryputs "Jeery is present"elseputs "Jeery is absent"end#false treated as falseTom = falseif Tomputs "Tom is present"elseputs "Tom is absent"end
0
.nil
and nil
class.false
and false
class.nil
is also treated as false
followed by the behavior of actual false
.