# Is this Ruby warning making sense?

**URL:** <https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451>\
**Category:** Ruby\
**Created:** [February 27, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451 "2006-02-27T23:48:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 27, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451/1 "2006-02-27T23:48:59Z")

</div>

Does anyone think if this the warning makes any sense?

irb(main):002:0\> puts “abc” if (a=true)  
(irb):2: warning: found = in conditional, should be ==  
abc

I thought in ruby, everything is an expression, so a=true should return  
true, and  
if (true) is totally valid. Why does it give the warning?

Or is this a language design thing?

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 27, 2006, 11:55pm UTC](https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451/2 "2006-02-27T23:55:07Z")

</div>

rubyrus wrote:

> Or is this a language design thing?

Any construction literal (true in this case) will produce such a warning  
since its value is already known,

but:

b=true  
puts “blah” if a=b

wont

lopex

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 4:26am UTC](https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451/3 "2006-02-28T04:26:57Z")

</div>

Writing ‘if (foo = “bar”)’ instead of ‘if (foo == “bar”)’ is a classic  
error that has bitten many programmers, since it can be difficult while  
quickly skimming the source code to determine whether the assignment  
was intentional or simply a typo.

Since it’s not technically invalid syntax, you just get a warning, but  
I wouldn’t recommend making heavy use of mixed assignment and test.

-Lennon

---

<div class="post-metadata">

**Author:** ![rubyrus](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/e9bcb4/32.png) [@rubyrus](https://www.ruby-forum.com/u/rubyrus)\
**Post date:** [February 28, 2006, 11:18am UTC](https://www.ruby-forum.com/t/is-this-ruby-warning-making-sense/74451/4 "2006-02-28T11:18:03Z")

</div>

rubyrus schrieb:

> Does anyone think if this the warning makes any sense?
> 
> irb(main):002:0\> puts “abc” if (a=true) (irb):2: warning: found = in  
> conditional, should be == abc

Yes, because you are testing an expression (a=true) though it’s obvious  
in  
this context that it will return true. This means that the `if` is  
meaningless for `puts "abc"`.

You won’t get the warning when you write `if true`, however. In this  
case  
Ruby assumes that you wrote this intenionally, while in the above case,  
the  
programmer might simply have failed to write ==.

Malte
