Warnings on assignments in conditionals?

  • Thread starter David Heinemeier Hansson
  • Start date
D

David Heinemeier Hansson

I consider the following statements to be perfectly legit (although
perhaps slightly ambigious) and Ruby does obey, but throws this
mandating warning. Am I just being paranoid or is Ruby forcing a style
upon me :)?

irb(main):008:0> if a = 1 then true else false end
(irb):8: warning: found = in conditional, should be ==
=> true
irb(main):009:0> a
=> 1

irb(main):010:0> if a = nil then true else false end
(irb):10: warning: found = in conditional, should be ==
=> false
irb(main):011:0> a
=> nil
 
A

Ara.T.Howard

Date: Wed, 14 Jan 2004 02:40:16 +0900
From: David Heinemeier Hansson <[email protected]>
Newsgroups: comp.lang.ruby
Subject: Warnings on assignments in conditionals?

I consider the following statements to be perfectly legit (although
perhaps slightly ambigious) and Ruby does obey, but throws this
mandating warning. Am I just being paranoid or is Ruby forcing a style
upon me :)?

irb(main):008:0> if a = 1 then true else false end
irb(main):008:0> if((a = 1)) then true else false end

(irb):8: warning: found = in conditional, should be ==
=> true
irb(main):009:0> a
=> 1

irb(main):010:0> if a = nil then true else false end
irb(main):010:0> if((a = nil)) then true else false end
(irb):10: warning: found = in conditional, should be ==
=> false
irb(main):011:0> a
=> nil


-a
--

ATTN: please update your address books with address below!

===============================================================================
| EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov
| PHONE :: 303.497.6469
| ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328
| STP :: http://www.ngdc.noaa.gov/stp/
| NGDC :: http://www.ngdc.noaa.gov/
| NESDIS :: http://www.nesdis.noaa.gov/
| NOAA :: http://www.noaa.gov/
| US DOC :: http://www.commerce.gov/
|
| The difference between art and science is that science is what we
| understand well enough to explain to a computer.
| Art is everything else.
| -- Donald Knuth, "Discover"
|
| /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done'
===============================================================================
 
M

Martin Weber

Oops, accidentally replied in private:


I consider the following statements to be perfectly legit (although
perhaps slightly ambigious) and Ruby does obey, but throws this
mandating warning. Am I just being paranoid or is Ruby forcing a style
upon me :)?

It is. The 'gcc' style.. an additional pair of parens around the
expression et voila (funny enough, you need two)

irb(main):001:0> if a = 1 then true else false end
(irb):1: warning: found = in conditional, should be ==
=> true
irb(main):002:0> if (a=1) then true else false end
(irb):2: warning: found = in conditional, should be ==
=> true
irb(main):003:0> if ((a=1)) then true else false end
=> true

Regards,

-Martin


..to which David replied:
Pretty interesting. Anyone has the full story on why?

/ David

Sorry.

-Martin
 
B

Bret Jolly

David Heinemeier Hansson said:
I consider the following statements to be perfectly legit (although
perhaps slightly ambigious) and Ruby does obey, but throws this
mandating warning. Am I just being paranoid or is Ruby forcing a style
upon me :)?

irb(main):008:0> if a = 1 then true else false end
(irb):8: warning: found = in conditional, should be ==
=> true
irb(main):009:0> a
=> 1

irb(main):010:0> if a = nil then true else false end
(irb):10: warning: found = in conditional, should be ==
=> false
irb(main):011:0> a
=> nil

But this C idiom really doesn't make much sense in Ruby, since
in Ruby 0 is true in a boolean context. You might want to say
something like:
if not (a = c).nil? then foo else bar end
but what you have is almost certainly either an error or at least
stylistically dubious. So yes, I guess Ruby is forcing a style on
you. I think that if you really want something like this, the Ruby
way would be to define a method 'true?' for objects:
class Object
def true?
if nil? or self == false then return false end
true
end
def false?
not true?
end
end

then write:
if (a = c).true? then foo else bar end

Oinkful Regards, Bret
 
R

Robert Klemme

David Heinemeier Hansson said:
I consider the following statements to be perfectly legit (although
perhaps slightly ambigious) and Ruby does obey, but throws this
mandating warning. Am I just being paranoid or is Ruby forcing a style
upon me :)?

irb(main):008:0> if a = 1 then true else false end
(irb):8: warning: found = in conditional, should be ==
=> true
irb(main):009:0> a
=> 1

irb(main):010:0> if a = nil then true else false end
(irb):10: warning: found = in conditional, should be ==
=> false
irb(main):011:0> a
=> nil

Just a side remark: assignment in conditional is only used in loops. "if"
never needs this. You can always put it onto two lines even gaining
readability:

a = 1
if a then ...

Besides of that you can do

a = 1 and ...

This will look familiar to P*rl users.

You only really need it in situations like this:

while ( line = gets )
...
end

Cheers

robert
 
R

Robert Klemme

Bret Jolly said:
David Heinemeier Hansson <[email protected]> wrote in message

But this C idiom really doesn't make much sense in Ruby, since
in Ruby 0 is true in a boolean context. You might want to say
something like:
if not (a = c).nil? then foo else bar end
but what you have is almost certainly either an error or at least
stylistically dubious. So yes, I guess Ruby is forcing a style on
you. I think that if you really want something like this, the Ruby
way would be to define a method 'true?' for objects:
class Object
def true?
if nil? or self == false then return false end
true
end
def false?
not true?
end
end

then write:
if (a = c).true? then foo else bar end

*shudder* You're not serious, are you? And if so, why didn't you define
them like this:

class Object
def true?; self; end
def false?; not self; end
end

Cheers

robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top