From: Ross Bamford Date: 2006-01-11T20:18:07+09:00 Subject: Re: Finding a sentence (more than one word & punctuation (, . ;))in a string? On Wed, 11 Jan 2006 10:52:08 -0000, Mark Woodward wrote: > Mark Woodward wrote: > ... >> # x seemed to include empty strings so only add x to the array if not >> "" >> foo.split(/\s*<[^<>]*>\s*/).reject{|x| a.insert(-1,x) if x != ""} > > Hmm, here's the first improvement? Seems I can use a << x to append to > an array: > > # x seemed to include ""??? so only add x to the array if not "" > foo.split(/\s*<[^<>]*>\s*/).reject{|x| a << x if x != ""} > I'm not sure what you're trying to do here, but I think split returns an array already, operated on by reject in this case, which returns the new array. So with the Erik's code: a = s.split(/\s*<[^<>]*>\s*/).reject{|x| x.empty?} p a # => ["message", "the message to echo.", ... etc ... ] I guess an alternative similar to your approach above might be: b = foo.split(/\s*<[^<>]*>\s*/).inject([]) { |ary,x| if x.empty? then ary else ary << x end } p b # => ["message", "the message to echo.", ... etc ... ] Note the 'p' method, which prints out using 'inspect'. Alternatively, you could have done: puts b.inspect print "{b.inspect}\n" and so on. Another nitpick about your example, is that in most Ruby I've seen people tend to prefer using unless rather than !negating the condition to if. So where you have: if x != "" I'd tend to use: unless x == "" or (more likely): unless x.empty? Cheers, -- Ross Bamford - rosco@roscopeco.remove.co.uk