Forum: Rails Germany Testen: warning: string literal in condition

Posted by Adam Meyer (outsida)
on 2009-02-24 23:20
Hallo,

beim Ausführen meiner Unittest bekomme ich die Warnung

"/Users/adam/Webs/kwikit/app/controllers/admin_controller.rb:80:
warning: string literal in condition"

Die genannte Zeile enthält folgenden Code:

text @prints[i].empfaenger.name + (@prints[i].empfaenger.company = ""?
"" : @prints[i].empfaenger.company + "\n") +
(@prints[i].empfaenger.address_additional = "" ? "" :
@prints[i].empfaenger.address_additional + "\n") + "\n" +
@prints[i].empfaenger.street + "\n" + @prints[i].empfaenger.zip + " " +
@prints[i].empfaenger.city + "\n\n" + @prints[i].empfaenger.country


Wahrscheinlich meckert er über die Condition in

(@prints[i].empfaenger.company = ""? "" : @prints[i].empfaenger.company
+ "\n")

Aber was hat er da zu meckern, verstehe ich nicht. Wieso ist da ein
Literal enthalten und wo ist das Problem damit?

Gruß

Adam
Posted by Rene Paulokat (Guest)
on 2009-02-24 23:46
(Received via mailing list)
On Tue, 24 Feb 2009 23:20:59 +0100
Adam Meyer <lists@ruby-forum.com> wrote:

> "" : @prints[i].empfaenger.company + "\n") +
> (@prints[i].empfaenger.address_additional = "" ? "" :
> @prints[i].empfaenger.address_additional + "\n") + "\n" +
> @prints[i].empfaenger.street + "\n" + @prints[i].empfaenger.zip + " " +
> @prints[i].empfaenger.city + "\n\n" + @prints[i].empfaenger.country
> 
> 
> Wahrscheinlich meckert er über die Condition in
> 
> (@prints[i].empfaenger.company = ""? "" : @prints[i].empfaenger.company
> + "\n")

weil '=' != '=='
oder weil du 'einistgleichzeichen' also eine zuweisung statt eine 
überprüfung vornimmst.

gruesse
rene
Posted by Peter Vandenabeele (petervandenabeele)
on 2009-02-24 23:52
(Received via mailing list)
Sorry to reply in English (my German knowled very limited ...).

On Tue, Feb 24, 2009 at 11:20 PM, Adam Meyer <lists@ruby-forum.com> 
wrote:
> text @prints[i].empfaenger.name + (@prints[i].empfaenger.company = ""?
> "" : @prints[i].empfaenger.company + "\n") +

Are you really sure you did not intend something like this (with double 
==):

   text @prints[i].empfaenger.name + (@prints[i].empfaenger.company == 
""?
   "" : @prints[i].empfaenger.company + "\n") + ...

If you are inside Rails (not in 1.8.6 Ruby, only in Rails), you could 
also
write more neatly:

   text @prints[i].empfaenger.name + 
(@prints[i].empfaenger.company.blank? ?
   "" : @prints[i].empfaenger.company + "\n") + ...

The major advantage of blank? is that it will trap both nil values and 
empty
strings and also is more readable (the nil values could easily come from 
a NULL
in the database, or missing key in a params[] parameter list).

HTH,

Peter
Posted by Adam Meyer (outsida)
on 2009-02-25 00:07
>> Wahrscheinlich meckert er �ber die Condition in
>> 
>> (@prints[i].empfaenger.company = ""? "" : @prints[i].empfaenger.company
>> + "\n")
> 
> weil '=' != '=='
> oder weil du 'einistgleichzeichen' also eine zuweisung statt eine 
> �berpr�fung vornimmst.
> 
> gruesse
> rene

Manchmal mehr Glück als Verstand! Danke!
Posted by Adam Meyer (outsida)
on 2009-02-25 00:08
>    text @prints[i].empfaenger.name + 
> (@prints[i].empfaenger.company.blank? ?
>    "" : @prints[i].empfaenger.company + "\n") + ...
> 
> The major advantage of blank? is that it will trap both nil values and 
> empty
> strings and also is more readable (the nil values could easily come from 
> a NULL
> in the database, or missing key in a params[] parameter list).
> 
> HTH,
> 
> Peter

Thx Peter. Funny, English speaking guy is answering German questions! =)
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.