From: georgesawyer Date: 2005-01-08T06:56:27+09:00 Subject: Re: what's the Ruby way to do this? At first, I thought: a ='The quick red fox jumps over the lazy brown fat hog.' p (b =a.split).size < 10 ?b :(b.first 10) <<'...' # Result: =>["The", "quick", "red", "fox", "jumps", "over", "the", "lazy", "brown", "fat", "..."] # Then I considered its real use and changed it to: p a='This is a test sentence that I just now made up.' b =a.strip last =-1 # Start before. 10.times do last =b.index( # This word's last character. %r{ # Regular expression specifying: \w # A word character, \b # Immediately followed by a word boundary. }x , # Ignore spaces and newlines above. last +1) # Start with next character. # Debug code: print last ,b[last].chr ,"\n" end p (last >=b.size) ?b :b[ 0..last] +'...' __END__ #Result: =>"This is a test sentence that I just now made up." =>"This is a test sentence that I just now made..."