Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS 

truncate Rails development/test logs

In rails applications development.log and test.log like to grow forever, which takes up space and makes them slow to grep. If I just delete them running processes with logs open might get confused. So I can use truncate instead:

truncate ~/www/*/log/*.log


Even easier, ask cron to do it for me every night:
4 22 * * * * truncate -s 0k  ~/www/*/log/*.log

Truncate text with word boundaries in Ruby

  def truncate_words(text, length = 30, end_string = '')
    words = text.split()
    words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
  end

Truncate String by Words

This will take a phrase and truncate it at the word level
<?php

function trunc($phrase, $max_words)
{
   $phrase_array = explode(' ',$phrase);
   if(count($phrase_array) > $max_words && $max_words > 0)
      $phrase = implode(' ',array_slice($phrase_array, 0, $max_words)).'...'  
   return $phrase;
}
?>
« Newer Snippets
Older Snippets »
Showing 1-3 of 3 total  RSS