What is the difference between NIL and FALSE in Ruby? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 16

What is the difference between NIL and FALSE in Ruby?

6th May 2019, 8:09 AM
MANUSHI
MANUSHI - avatar
4 Answers
+ 4
The NIL and FALSE objects in Ruby have different purposes. The first is used to resolve reference errors neatly. The last is important for control structures (if, elseif, while, until, unless). Check out the links to get a better understanding. . https://skorks.com/2009/09/true-false-and-nil-objects-in-ruby/ . https://en.wikipedia.org/wiki/Null_object_pattern . http://ruby-doc.org/core-2.6.3/NilClass.html . http://ruby-doc.org/core-2.6.3/FalseClass.html
6th May 2019, 10:32 AM
couscous
+ 3
That's ok Mr.In_finite
11th May 2019, 5:11 PM
MANUSHI
MANUSHI - avatar
+ 2
Both nil & false are objects, they have different meanings. False is a logical false. Example: "Is today a windy day?" Answer is either yes (true) or no (false). That's the whole purpose of false. Now: Nil is used in a different context, the context is that of when you're trying to find something in a list of many & nothing can be found. In other words, nil represents the concept of "nothing" or "empty" in Ruby. For example: values = ["a", "b", "c"] values[9] This returns "nil" because there is no value at index 9 in the "values" array. You can learn more here: - https://www.rubyguides.com/2018/01/ruby-nil/ - https://www.rubyguides.com/2019/02/ruby-booleans/ - https://gist.github.com/pythonicrubyist/8114720 Let me know if you have more questions :)
11th May 2019, 5:46 PM
Jesus Castello
Jesus Castello - avatar
+ 1
It may depend. False may be 0, true may be any number other than 0, while nil is none. however, this is only referring to individual values. There are other ways to use false and nil.
6th May 2019, 6:18 PM
Choe
Choe - avatar