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

143cca9f26d2421816f3758638b74647?d=identicon&s=25 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
A6ebaaa00666e4f1934d9ca2c024aaf3?d=identicon&s=25 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
160992dfd434ff7055465446b9423bd5?d=identicon&s=25 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
143cca9f26d2421816f3758638b74647?d=identicon&s=25 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!
143cca9f26d2421816f3758638b74647?d=identicon&s=25 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! =)
This topic is locked and can not be replied to.