Testen: warning: string literal in condition


#1

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


#2

On Tue, 24 Feb 2009 23:20:59 +0100
Adam M. removed_email_address@domain.invalid 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


#3

Sorry to reply in English (my German knowled very limited …).

On Tue, Feb 24, 2009 at 11:20 PM, Adam M. removed_email_address@domain.invalid
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


#4

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! =)


#5

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!