Forum: Ruby simple question re: ruby warning message

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-19 10:00
(Received via mailing list)
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 >> (email)
<< TomCloyd.com >> (website)
<< sleightmind.wordpress.com >> (mental health weblog)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2009-02-19 10:04
(Received via mailing list)
From: Tom Cloyd [mailto:tomcloyd@comcast.net]
# db_type= 'n' ? db = @nodes_h : db = @links_h

        ^^^

try replacing  =  with ==
C40020a47c6b625af6422b5b1302abaf?d=identicon&s=25 Stefano Crocco (crocco)
on 2009-02-19 10:05
(Received via mailing list)
Alle Thursday 19 February 2009, Tom Cloyd 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
D15a45a973443d4562051eb675b60474?d=identicon&s=25 Tom Cloyd (Guest)
on 2009-02-19 10:09
(Received via mailing list)
Stefano Crocco 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.

--

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
6087a044557d6b59ab52e7dd20f94da8?d=identicon&s=25 Peña, Botp (Guest)
on 2009-02-19 10:11
(Received via mailing list)
From: Peña, Botp [mailto:botp@delmonte-phil.com]
#
# From: Tom Cloyd [mailto:tomcloyd@comcast.net]
# # 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 ==
6b46f4eed76df33a3ffacf87bc9f1079?d=identicon&s=25 vimal (Guest)
on 2009-02-19 10:20
(Received via mailing list)
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

 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
8f6f95c4bd64d5f10dfddfdcd03c19d6?d=identicon&s=25 Rick Denatale (rdenatale)
on 2009-02-19 18:19
(Received via mailing list)
On Thu, Feb 19, 2009 at 4:19 AM, vimal <cool.vimalsmail@gmail.com>
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/
Twitter: http://twitter.com/RickDeNatale
WWR: http://www.workingwithrails.com/person/9021-rick-denatale
LinkedIn: http://www.linkedin.com/in/rickdenatale
This topic is locked and can not be replied to.