ruby - Get first N characters from string without cutting the whole words -


i want know if there easy way n symbols string without cutting whole words.

for example, have products , products descriptions information. description length 70 500 symbols, want display first 70 symbols this:

coca-cola popular , biggest-selling soft drink in history, best-known brand in world.

on may 8, 2011, coca-cola celebrated 125thanniversary. created in 1886 in atlanta, georgia, dr. john s. pemberton, coca-cola first offered fountain beverage @ jacob's pharmacy mixing coca-cola syrup carbonated water.

so, ordinary sub string method give me:

coca-cola popular , biggest-selling soft drink in histor 

and need method this:

coca-cola popular , biggest-selling soft drink in ... 

s = "coca-cola popular , biggest-selling soft drink in history, best-known brand in world." s = s.split(" ").each_with_object("") {|x,ob| break ob unless (ob.length + " ".length + x.length <= 70);ob << (" " + x)}.strip #=> "coca-cola popular , biggest-selling soft drink in" 

Comments