Warnings on assignments in conditionals?

  • Thread starter David Heinemeier Hansson
  • Start date
D

David Heinemeier Hansson

Apologies in advance if you already understand this distinction, but
it is one that has tripped many a programmer, even those who should
know better ;) If you indeed mean to assign the value to 'a' (as in
the first example), you can probably avoid the warning from Ruby by
breaking this into two lines, e.g.

a = 1
if a then true else false end

I did understand the distinction, I was just puzzled that Ruby would
throw a warning on something that worked as intended and as advertised
although it would probably be considered ambigious.

But in this real-life example, it doesn't that seem ambigious (and
hence useful):

def apply_coupon(code)
if @coupon = Coupon.find_by_code(code) then true else false end
end

I guess I'm questioning when, or even if, the parser should throw
warnings on something that it considers to be bad style.

/ David
 
J

Jamis Buck

David said:
I did understand the distinction, I was just puzzled that Ruby would
throw a warning on something that worked as intended and as advertised
although it would probably be considered ambigious.

But in this real-life example, it doesn't that seem ambigious (and
hence useful):

def apply_coupon(code)
if @coupon = Coupon.find_by_code(code) then true else false end
end

I guess I'm questioning when, or even if, the parser should throw
warnings on something that it considers to be bad style.

/ David


For what its worth, you can get the same effect, without the warning,
like this:

@coupon = Coupon.find_by_code(code) and true or false

Not nearly as readable, but...

Alternatively, you can take advantage of the fact that Ruby treats all
values as 'true' EXCEPT 'nil' and 'false' (which are always false), you
could just say

def apply_coupon( code )
@couple = Coupon.find_by_code(code)
end

This would then return the value of @coupon, which (if null or false)
would be false, and otherwise may be treated as a 'true' value.
 
Y

YANAGAWA Kazuhisa

In Message-Id: <[email protected]>
David Heinemeier Hansson said:
def apply_coupon(code)
if @coupon = Coupon.find_by_code(code) then true else false end
end

I guess I'm questioning when, or even if, the parser should throw
warnings on something that it considers to be bad style.
ruby -wc -e 'if str = ""; end'
-e:1: warning: found = in conditional, should be ==
Syntax OK
ruby -wc -e 'if str = gets; end' Syntax OK
ruby -v
ruby 1.8.0 (2003-09-09) [i386-freebsd4]

So I'm not sure but at least 1.8.0, only assigning a literal in a
condition is warned for my understanding.

That can be pointed out as a mistake on high probability, isn't it?
 

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,786
Messages
2,569,625
Members
45,320
Latest member
icelord

Latest Threads

Top