class String

Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>

Copyright Freya Dorn <freya.siv.dorn@gmail.com>, 2013 License: GNU GPL 3 <www.gnu.org/copyleft/gpl.html>

Constants

ANSIColorRegexp
FULLWIDTH_KANA
HALFWIDTH_KANA

Public Instance Methods

color(name) click to toggle source
# File lib/muflax/string.rb, line 66
def color name
  HighLine.color(self, name)
end
first(limit=1) click to toggle source
# File lib/muflax/string.rb, line 42
def first limit=1
  if limit == 0       ; ""
  elsif limit >= size ; dup
  else                ; self[0..(limit - 1)]
  end
end
fullwidth() click to toggle source
# File lib/muflax/string.rb, line 73
def fullwidth
  tr(" !-~" + HALFWIDTH_KANA, "\u3000" + (0xFF01...0xFF5f).to_a.pack('U*') + FULLWIDTH_KANA)
end
gsub_all(replacements={})
Alias for: mgsub
gsub_all!(replacements={})
Alias for: mgsub!
indent(amount, indent_string = nil, indent_empty_lines = false) click to toggle source
# File lib/muflax/string.rb, line 62
def indent amount, indent_string = nil, indent_empty_lines = false
  dup.tap { |_| _.indent!(amount, indent_string, indent_empty_lines) }
end
indent!(amount, indent_string = nil, indent_empty_lines = false) click to toggle source
# File lib/muflax/string.rb, line 56
def indent! amount, indent_string = nil, indent_empty_lines = false
  indent_string = indent_string || self[/^[ \t]/] || " "
  re = indent_empty_lines ? /^/ : /^(?!$)/
  gsub!(re, indent_string * amount)
end
last(limit=1) click to toggle source
# File lib/muflax/string.rb, line 49
def last limit=1
  if limit == 0       ; ""
  elsif limit >= size ; dup
  else                ; self[(-limit)..-1]
  end
end
mgsub(replacements={}) click to toggle source
# File lib/muflax/regex.rb, line 7
def mgsub replacements={}
  str = self.dup

  str.mgsub! replacements

  str
end
Also aliased as: gsub_all
mgsub!(replacements={}) click to toggle source
# File lib/muflax/regex.rb, line 16
def mgsub! replacements={}
  replacements.each do |a, b|
    self.gsub! a, b
  end

  self
end
Also aliased as: gsub_all!
truncate(length, omission: "...") click to toggle source
# File lib/muflax/string.rb, line 16
def truncate length, omission: "..."
  return self if self.length <= length

  ss          = StringScanner.new(self)
  out         = ss.scan(ANSIColorRegexp) || ""
  cut_length  = length - omission.str_length
  used        = 0
  cut_at      = 0

  while used < length and not ss.eos?
    out << ss.getch
    c = ss.scan(ANSIColorRegexp)
    out << c unless c.nil?
    used += 1

    cut_at = out.length if used == cut_length
  end

  if not ss.eos?
    out = out[0, cut_at]
    out << omission
  end

  out
end
whackuum(str=/\s+/) click to toggle source
# File lib/muflax/string.rb, line 10
def whackuum str=/\s+/
  self.split(str).map(&:strip)
end