From: Mark Woodward Date: 2006-01-11T20:43:05+09:00 Subject: Re: Finding a sentence (more than one word & punctuation (, . ;))in a string? Hi Ross, Ross Bamford wrote: > On Wed, 11 Jan 2006 10:52:08 -0000, Mark Woodward > wrote: > I'm not sure what you're trying to do here, makes 2 of us ;-) 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 ... ] Exactly what I was trying to do. I thought it had to be an array but couldn't figure out how to print it like ["","",""] like the OP wanted. p a - now thats embarrassing! 2 letters and it works. Compare that to my gibberish :-(. We all have to start somewhere I guess! > > 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" steady on! ;-) > > 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? Nitpick away! I appreciate it. Its been a good little exercise re p, puts, print and chaining methods etc. I've been reading the pickaxe book, but readings not good enough. I need to write some code. If I can make a fool of myself here but learn something at the same time then thats great! > > Cheers, > thanks, -- Mark