Ruby snippet for flexible blurbing of text

Here’s a cute little method I use for extracting blurbs from text. Specify a minimum character count as the argument, and the method returns a string containing the minimum text plus whatever it takes to reach the next period, question mark, or exclamation point.

def blurb(size=75)
  reg = Regexp.new ".{#{size}}[,;:-_\\w\\s]*[\.\!\?]"
  body.slice(reg)
end
About these ads
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: