# Simple question re: ruby warning message

**URL:** <https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748>\
**Category:** Ruby\
**Created:** [February 19, 2009, 10:00am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748 "2009-02-19T10:00:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:00am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/1 "2009-02-19T10:00:23Z")

</div>

I just set up my first ternary operator expression, and got a warning  
from Ruby, as a reward! I don’t see what the problem might be:

The expression:  
db\_type= ‘n’ ? db = @nodes\_h : db = @links\_h

The warning:  
…/lib/setnet/SN.rb:781: warning: string literal in condition

I use string literals all the time in case statements, and other places,  
and have never had this objection.

Can someone explain the problem to me? I cannot imagine what it might  
be.

Thanks!

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:05am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/2 "2009-02-19T10:05:11Z")

</div>

Alle Thursday 19 February 2009, Tom C. ha scritto:

> and have never had this objection.
> 
> Can someone explain the problem to me? I cannot imagine what it might be.
> 
> Thanks!
> 
> t.

Ruby thinks you are likely to have written = instead of == in the  
condition.

Stefano

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:09am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/3 "2009-02-19T10:09:17Z")

</div>

Stefano C. wrote:

> > 
> 
> Ruby thinks you are likely to have written = instead of == in the condition.
> 
> Stefano

Geee, I wonder why? Good Lord!

A real newbie mistake!

t.

–

```auto
Tom C., MS MA, LMHC - Private practice Psychotherapist
Bellingham, Washington, U.S.A: (360) 920-1226
<< removed_email_address@domain.invalid >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)

```

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:04am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/4 "2009-02-19T10:04:35Z")

</div>

From: Tom C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# db\_type= ‘n’ ? db = @nodes\_h : db = @links\_h

```
    ^^^

```

try replacing = with ==

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:11am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/5 "2009-02-19T10:11:04Z")

</div>

From: Peña, Botp [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# 

# From: Tom C. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# # db\_type= ‘n’ ? db = @nodes\_h : db = @links\_h

# 

# ^^^

# 

# try replacing = with ==

# 

or maybe better do some arrangement, like eg,

db = db\_type==‘n’ ? @nodes\_h : @links\_h

and wc clearly shows diff bw = and ==

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 6:19pm UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/6 "2009-02-19T18:19:22Z")

</div>

On Thu, Feb 19, 2009 at 4:19 AM, vimal [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> ```
> end
> 
> ```

Which, since any ruby object except nil and false is truthy is  
effectively  
the same thing as

db\_type = db = @nodes.h

which makes db\_type, and db reference the same object, which is the  
return  
valud of @nodes.h

–  
Rick DeNatale

Blog: [http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)  
Twitter: [http://twitter.com/RickDeNatale](http://twitter.com/RickDeNatale)  
WWR: [http://www.workingwithrails.com/person/9021-rick-denatale](http://www.workingwithrails.com/person/9021-rick-denatale)  
LinkedIn: [http://www.linkedin.com/in/rickdenatale](http://www.linkedin.com/in/rickdenatale)

---

<div class="post-metadata">

**Author:** ![tbcox23](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/b9bd4f/32.png) [@tbcox23](https://www.ruby-forum.com/u/tbcox23)\
**Post date:** [February 19, 2009, 10:20am UTC](https://www.ruby-forum.com/t/simple-question-re-ruby-warning-message/160748/7 "2009-02-19T10:20:28Z")

</div>

On Feb 19, 1:59 pm, Tom C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I just set up my first ternary operator expression, and got a warning  
> from Ruby, as a reward! I don’t see what the problem might be:
> 
> The expression:  
> db\_type= ‘n’ ? db = @nodes\_h : db = @links\_h

I guess your representation (db\_type= ‘n’)  
doesn’t look like a condition, its like performing an assignment.

If its a condition it should be db\_type == ‘n’

Your representation (db\_type= ‘n’ ? db = @nodes\_h : db = @links\_h)

assigns db\_type the result of the following:

checks a condition ‘n’  
if true  
db = @nodes\_h  
else  
db = @links\_h  
end

Regards,  
Vimal Das
