Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/merb_helpers/core_ext.rb

Overview

Truncates a string to the given length and appends the given suffix if the string is, in fact, truncated.

Examples:

"This is a long string right here".truncate(10, "...")  #=> "This is..."

Instance Method Summary collapse

Instance Method Details

#truncate(length = 30, truncate_string = "...") ⇒ Object



41
42
43
44
45
# File 'lib/merb_helpers/core_ext.rb', line 41

def truncate(length = 30, truncate_string = "...")
  return self unless self.length > length
  length = length - truncate_string.split(//).length
  self[0...length] + truncate_string
end