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



52
53
54
55
56
# File 'lib/merb-helpers/core_ext.rb', line 52

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