# Simple question re: ruby warning message

**URL:** <https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829>\
**Category:** ruby-talk\
**Created:** [19 February 2009 08:59 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829 "2009-02-19T08:59:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [19 February 2009 08:59 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/1 "2009-02-19T08:59:21Z")

</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.

> **···**
>
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist  
> Bellingham, Washington, U.S.A: (360) 920-1226  
> \<\< [tc@tomcloyd.com](mailto:tc@tomcloyd.com) \>\> (email)  
> \<\< [TomCloyd.com](http://TomCloyd.com) \>\> (website) \<\< [sleightmind.wordpress.com](http://sleightmind.wordpress.com) \>\> (mental health weblog)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [19 February 2009 09:03 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/2 "2009-02-19T09:03:30Z")

</div>

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

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;^^^

try replacing = with ==

> **···**
>
> From: Tom Cloyd [[mailto:tomcloyd@comcast.net](mailto:tomcloyd@comcast.net)]

---

<div class="post-metadata">

**Author:** ![Stefano\_Crocco](https://avatars.discourse-cdn.com/v4/letter/s/5f9b8f/32.png) [@Stefano\_Crocco](https://rubytalk.org/u/Stefano_Crocco)\
**Post date:** [19 February 2009 09:03 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/3 "2009-02-19T09:03:49Z")

</div>

Alle Thursday 19 February 2009, Tom Cloyd ha scritto:

> 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.

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

Stefano

---

<div class="post-metadata">

**Author:** ![vimal](https://avatars.discourse-cdn.com/v4/letter/v/d2c977/32.png) [@vimal](https://rubytalk.org/u/vimal)\
**Post date:** [19 February 2009 09:19 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/4 "2009-02-19T09:19:51Z")

</div>

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)

&nbsp;&nbsp;assigns db\_type the result of the following:

&nbsp;&nbsp;&nbsp;checks a condition 'n'  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = @nodes\_h  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;else  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = @links\_h  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

Regards,  
Vimal Das

> **···**
>
> On Feb 19, 1:59 pm, Tom Cloyd \<tomcl...@comcast.net\> 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

---

<div class="post-metadata">

**Author:** ![Tom\_Cloyd2](https://avatars.discourse-cdn.com/v4/letter/t/6a8cbe/32.png) [@Tom\_Cloyd2](https://rubytalk.org/u/Tom_Cloyd2)\
**Post date:** [19 February 2009 09:07 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/5 "2009-02-19T09:07:32Z")

</div>

Stefano Crocco wrote:

> Alle Thursday 19 February 2009, Tom Cloyd ha scritto:  
> &nbsp;&nbsp;
> 
> > 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.  
> > &nbsp;&nbsp;&nbsp;&nbsp;  
> > 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.

> **···**
>
> --
> 
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~  
> Tom Cloyd, MS MA, LMHC - Private practice Psychotherapist  
> Bellingham, Washington, U.S.A: (360) 920-1226  
> \<\< tc@tomcloyd.com \>\> (email)  
> \<\< TomCloyd.com \>\> (website) \<\< sleightmind.wordpress.com \>\> (mental health weblog)  
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

---

<div class="post-metadata">

**Author:** ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)\
**Post date:** [19 February 2009 09:09 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/6 "2009-02-19T09:09:46Z")

</div>



---

<div class="post-metadata">

**Author:** ![Rick\_DeNatale1](https://avatars.discourse-cdn.com/v4/letter/r/bbce88/32.png) [@Rick\_DeNatale1](https://rubytalk.org/u/Rick_DeNatale1)\
**Post date:** [19 February 2009 17:18 UTC](https://rubytalk.org/t/simple-question-re-ruby-warning-message/51829/7 "2009-02-19T17:18:33Z")

</div>

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

&nbsp;&nbsp;db\_type = db = @nodes.h

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

> **···**
>
> On Thu, Feb 19, 2009 at 4:19 AM, vimal \<cool.vimalsmail@gmail.com\> wrote:
> 
> > Your representation (db\_type= 'n' ? db = @nodes\_h : db = @links\_h)
> > 
> > assigns db\_type the result of the following:
> > 
> > &nbsp;&nbsp;checks a condition 'n'  
> > &nbsp;&nbsp;&nbsp;&nbsp;if true  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = @nodes\_h  
> > &nbsp;&nbsp;&nbsp;&nbsp;else  
> > &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;db = @links\_h  
> > &nbsp;&nbsp;&nbsp;&nbsp;end
> 
> --  
> 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)
