class String

Constants

LATIN_MAP

Map of latin chars and their representations as unicode chars.

Public Instance Methods

%(args) click to toggle source
# File lib/vidibus/core_extensions/string.rb, line 77
def %(args)
  if args.kind_of?(Hash)
    ret = dup
    args.each do |key, value|
      ret.gsub!(/\%\{#{key}\}/, value.to_s)
    end
    ret
  else
    ret = gsub(/%\{/, '%%{')
    begin
      ret._sprintf(args)
    rescue ArgumentError
      $stderr.puts "  The string:#{ret}"
      $stderr.puts "  args:#{args.inspect}"
    end
  end
end
Also aliased as: _sprintf
_sprintf(args)

Extends Kernel::sprintf to accept “named arguments” given as hash. This method was taken from Ruby-GetText. Thank you!

Examples:

# Normal sprintf behaviour:
"%s, %s" % ["Masao", "Mutoh"]

# Extended version with named arguments:
"%{firstname}, %{familyname}" % {:firstname => "Masao", :familyname => "Mutoh"}
Alias for: %
latinize() click to toggle source

Replaces non-latin chars, leaves some special ones.

# File lib/vidibus/core_extensions/string.rb, line 47
def latinize
  c = dup
  for char, map in LATIN_MAP
    c.gsub!(/[#{map.join}]/mu, char)
  end
  c.gsub!(/[^a-zA-Z0-9\.\,\|\?\!\:;"'=\+\-_]+/mu, " ")
  c.gsub!(/\s+/, " ")
  c
end
snip(length, ellipsis = "…") click to toggle source

Truncates string to given length while preserves whole words. If string exceeds given length, an ellipsis will be appended.

Example:

“O Brother, Where Art Thou?”.snip(13) # => “O Brother, Where…”

# File lib/vidibus/core_extensions/string.rb, line 102
def snip(length, ellipsis = "…")
  return self if self.empty?
  str = dup
  str.strip!
  str.gsub(/^(.{#{length.to_i-1}})(.)([\w]*)(.*)/m) do
    if $2 == " "
      "#{$1}#{ellipsis}"
    elsif $3.empty? and $4.empty?
      str
    else
      "#{$1}#{$2}#{$3}#{ellipsis}"
    end
  end
end
sortable() click to toggle source

Converts string into a naturally sortable string.

# File lib/vidibus/core_extensions/string.rb, line 140
def sortable
  matches = self.scan(/(\D+)?(\d+(?:[\.]?\d+)?)(\D+)?/)
  return self.downcase.gsub(/\s/, '') if matches.none?
  ''.tap do |converted|
    matches.each do |s1, i, s2|
      converted << s1.downcase.gsub(/\s/, '') if s1
      converted << '%030f' % i.to_f
      converted << s2.downcase.gsub(/\s/, '') if s2
    end
  end
end
strip_tags() click to toggle source

Removes all html tags from given string.

# File lib/vidibus/core_extensions/string.rb, line 118
def strip_tags
  self.gsub(/<+\/?[^>]*>+/, '')
end
strip_tags!() click to toggle source

Removes all html tags on self.

# File lib/vidibus/core_extensions/string.rb, line 123
def strip_tags!
  self.replace strip_tags
end
with_params(params = {}) click to toggle source

Appends hash of query params to current string.

Example:

vidibus.org”.with_params(:awesome => “yes”) # => “vidibus.org?awesome=yes

# File lib/vidibus/core_extensions/string.rb, line 134
def with_params(params = {})
  return self unless params and params.any?
  self + (self.match(/\?/) ? "&" : "?") + params.to_query
end