From: Robert Klemme Date: 2004-09-23T17:49:34+09:00 Subject: Re: whitespace string only "Henrik Horneber" schrieb im Newsbeitrag news:4152820A.9080408@gmx.net... > 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 > > >> rx = %r{\A\s*\z} => /\A\s*\z/ >> rx =~ "" => 0 >> rx =~ " " => 0 >> rx =~ " a" => nil >> rx =~ " \n a" => nil >> rx =~ " \n " => 0 >> rx =~ " \n \n" => 0 Regards robert