# Quick newbie true/false/nil question

**URL:** <https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465>\
**Category:** Ruby\
**Created:** [February 13, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465 "2007-02-13T21:25:49Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:25pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/1 "2007-02-13T21:25:49Z")

</div>

I ran across this line of code today, and it doesn’t make sense to me.

if ((klass = CommDispatcher.check\_hash(path)) == nil)

How would that value ever equal nil? I thought that if klass failed to  
be  
assigned to the value of CommDispatcher.check\_hash(path) then the value  
would be false and if it was assigned to the value of  
CommDispatcher.check\_hash(path) it would be true… How would it be nil?

Thanks!

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:30pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/2 "2007-02-13T21:30:50Z")

</div>

On Feb 13, 2:24 pm, “Jason M.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I ran across this line of code today, and it doesn’t make sense to me.
> 
> if ((klass = CommDispatcher.check\_hash(path)) == nil)
> 
> How would that value ever equal nil? I thought that if klass failed to be  
> assigned to the value of CommDispatcher.check\_hash(path) then the value  
> would be false and if it was assigned to the value of  
> CommDispatcher.check\_hash(path) it would be true… How would it be nil?
> 
> Thanks!

I’m not familiar with the CommDispatcher class, but it looks like  
klass will be assigned whatever the return value of check\_hash()  
returns, whether that’s nil or false or something else.

Jeff

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:35pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/3 "2007-02-13T21:35:50Z")

</div>

On 2/13/07, Jeff [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > assigned to the value of CommDispatcher.check\_hash(path) then the value  
> > would be false and if it was assigned to the value of  
> > CommDispatcher.check\_hash(path) it would be true… How would it be nil?
> > 
> > Thanks!
> 
> I’m not familiar with the CommDispatcher class, but it looks like  
> klass will be assigned whatever the return value of check\_hash()  
> returns, whether that’s nil or false or something else.
> 
> Jeff

Right… but isn’t the assignment of the value ‘nil’ going to return  
true  
since the assignment succeeded? I remember screwing up a program by  
forgetting to put in the extra = sign once… my if blah = blah2  
statement  
always returned true because the assignment occurred.

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:39pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/4 "2007-02-13T21:39:04Z")

</div>

Jason M. wrote:

> I ran across this line of code today, and it doesn’t make sense to me.
> 
> if ((klass = CommDispatcher.check\_hash(path)) == nil)

I don’t know anything about CommDispatcher, but I’d assume you could  
rewrite the above code like so:

if (!CommDispatcher.check\_hash(path))

The code above seems like a pretty bad code example.

-Drew

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:42pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/5 "2007-02-13T21:42:53Z")

</div>

Like in C, assignment returns IIRC the result of the assignment, so:

( (a = 2+3) == 5 ) == true

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/6 "2007-02-13T21:54:53Z")

</div>

On 2/13/07, SonOfLilit [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Like in C, assignment returns IIRC the result of the assignment, so:
> 
> ( (a = 2+3) == 5 ) == true

heh. I should just test it for myself before posting to the mailing  
list.

var1 = 1  
var2 = 2  
if var1 = var2  
puts “First if statement = true: #{var1} = #{var2}”  
else  
puts “First if statement = false: #{var1} != #{var2}”  
end  
var1 = 1  
var2 = 2  
if ((var1 = var2) == true)  
puts “Second if statement = true: #{var1} = #{var2} = true”  
else  
puts “Second if statement = false: #{var1} = #{var2} != true”  
end  
var1 = 1  
var2 = 2  
if ((var1 = var2) == nil)  
puts "Third if statement = true: #{var1} = #{var2} = nil "  
else  
puts “Third if statement = false: #{var1} = #{var2} != nil”  
end  
var1 = 1  
var2 = nil  
if ((var1 = var2) == nil)  
puts "Fourth if statement = true: #{var1} = #{var2} = nil "  
else  
puts “Fourth if statement = false: #{var1} = #{var2} != nil”  
end  
#output  
First if statement = true: 2 = 2  
Second if statement = false: 2 = 2 != true  
Third if statement = false: 2 = 2 != nil  
Fourth if statement = true: = = nil

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 9:53pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/7 "2007-02-13T21:53:53Z")

</div>

On Feb 13, 2007, at 3:34 PM, Jason M. wrote:

> Right… but isn’t the assignment of the value ‘nil’ going to  
> return true  
> since the assignment succeeded? I remember screwing up a program by  
> forgetting to put in the extra = sign once… my if blah = blah2  
> statement  
> always returned true because the assignment occurred.

The value associated with an assignment statement is the value of  
the right hand side of the assignment.

It is somewhat nonsensical to ask whether an assignment ‘succeeds’ or  
not.  
The only way for an assignment to fail would be a syntax error or some  
sort of exception while evaluating the right hand side, in which case  
the assignment won’t be evaluated as an expression at all.

It may help to remember that nil is simply a literal reference to a  
particular object in the same way that 1 is a literal reference for a  
particular Fixnum object (the one that behaves like the number 1).

Don’t think of nil as meaning the absence of any object but instead  
think of it as the unique object known as nil.

Gary W.

---

<div class="post-metadata">

**Author:** ![jasonmorrison](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/47e85d/32.png) [@jasonmorrison](https://www.ruby-forum.com/u/jasonmorrison)\
**Post date:** [February 13, 2007, 10:20pm UTC](https://www.ruby-forum.com/t/quick-newbie-true-false-nil-question/85465/8 "2007-02-13T22:20:57Z")

</div>

On Feb 13, 9:39 pm, Drew O. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The code above seems like a pretty bad code example.
> 
> -Drew
> 
> –  
> Posted viahttp://www.ruby-forum.com/.

The above code is certainly not a nice-looking one but that was not  
the question.

I suppose that the puzzling part was that the variable klass will be  
assigned with return value from CommDispatcher.check\_hash(path) and  
that that value could be nil.  
It is true, if we presume that it is a possible return value from  
CommDispatcher.check\_hash method.

The assignment inside the if condition is not a good but it is a  
common construction especially if you come form C++ world.  
Ruby has far more interesting constructions but it all depends on a  
context and mostly on programming style.

Generally this assignment inside the if condition should be avoided.

Dima
