+ 16
What is the difference between NIL and FALSE in Ruby?
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
+ 3
That's ok Mr.In_finite
+ 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 :)
+ 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.