def only_whitespace?
each_byte { |b| return false if b != 32 }
true
end
On Thu, 2004-09-23 at 00:54, Henrik Horneber wrote:
> 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
>