Anything wrong with

            if  "#{s}".chomp.strip.length == 0

or have I missed the point ?




Internet
ryco / gmx.net - 23/09/2004 09:54


Veuillez rŮ—ondre ruby-talk / ruby-lang.org

Pour : ruby-talk

cc :


Objet :     whitespace string only


Hi!

What's the best way to test if a string only consists of whitespaces and
newlines?

best I could come up with is


class String

   def is_whitespace_only?
     strings_to_test = split("\n")
     whitespace = /^\s+$/
     is_whitespace_only = true
     strings_to_test.each{ |str|
       unless whitespace.match(str) or str.empty?
         is_whitespace_only = false
 break
       end
     }
     is_whitespace_only
   end

end

But somehow I think there should be a better way to do it. Any ideas?
Is it okay to add such methods to class String itself?

Any advices appreciated.

regards,
Henrik










This message and any attachments (the "message") is
intended solely for the addressees and is confidential. 
If you receive this message in error, please delete it and 
immediately notify the sender. Any use not in accord with 
its purpose, any dissemination or disclosure, either whole 
or partial, is prohibited except formal approval. The internet
can not guarantee the integrity of this message. 
BNP PARIBAS (and its subsidiaries) shall (will) not 
therefore be liable for the message if modified. 

                ---------------------------------------------

Ce message et toutes les pieces jointes (ci-apres le 
"message") sont etablis a l'intention exclusive de ses 
destinataires et sont confidentiels. Si vous recevez ce 
message par erreur, merci de le detruire et d'en avertir 
immediatement l'expediteur. Toute utilisation de ce 
message non conforme a sa destination, toute diffusion 
ou toute publication, totale ou partielle, est interdite, sauf 
autorisation expresse. L'internet ne permettant pas 
d'assurer l'integrite de ce message, BNP PARIBAS (et ses
filiales) decline(nt) toute responsabilite au titre de ce 
message, dans l'hypothese ou il aurait ete modifie.