module RujitsuString

Public Instance Methods

to_url() click to toggle source

Return a string that can be used as part of a url replaces basic “bad” characters with “-”

  # File lib/rujitsu/string.rb
6 def to_url
7   self.downcase.gsub(/[^\-0-9a-z ]/, '').split.join('-')
8 end
truncate(opts = {}) click to toggle source

Truncates a string to the specified length, and appends suffix if required

Options:

  • length length to truncate string to. Includes the suffix in the length. Default is 50.

  • suffix suffix to append to truncated string. Pass “” or false for no suffix. Default is “…”.

   # File lib/rujitsu/string.rb
17 def truncate opts = {}
18   opts[:length] ||= 50
19   opts[:suffix] = opts.has_key?(:suffix) ? opts[:suffix] : "..."
20   opts[:suffix] ||= ""
21   opts[:length] -= (opts[:suffix].length+1)
22   if opts[:length] > 0
23     self.length > opts[:length] ? self[0..opts[:length]] + opts[:suffix] : self
24   else
25     opts[:suffix][0..(opts[:length] += opts[:suffix].length)]
26   end
27 end