class String

Public Instance Methods

_ljust(length, padstr=nil, rstr="")
Alias for: ljust
background_color(color=:red) click to toggle source
# File lib/flumtter/app/core/string.rb, line 36
def background_color(color=:red)
  case color
  when :black
    "\e[40m#{self}\e[0m"
  when :red
    "\e[41m#{self}\e[0m"
  when :green
    "\e[42m#{self}\e[0m"
  when :yellow
    "\e[43m#{self}\e[0m"
  when :blue
    "\e[44m#{self}\e[0m"
  when :magenta
    "\e[45m#{self}\e[0m"
  when :cyan
    "\e[46m#{self}\e[0m"
  when :white
    "\e[47m#{self}\e[0m"
  end
end
color(color=:red) click to toggle source
# File lib/flumtter/app/core/string.rb, line 15
def color(color=:red)
  case color
  when :white       then "\e[1;37m#{self}\e[0m"
  when :light_gray  then "\e[37m#{self}\e[0m"
  when :gray        then "\e[1;30m#{self}\e[0m"
  when :back        then "\e[30m#{self}\e[0m"
  when :red         then "\e[31m#{self}\e[0m"
  when :light_red   then "\e[1;31m#{self}\e[0m"
  when :green       then "\e[32m#{self}\e[0m"
  when :light_green then "\e[1;32m#{self}\e[0m"
  when :brown       then "\e[33m#{self}\e[0m"
  when :yellow      then "\e[1;33m#{self}\e[0m"
  when :blue        then "\e[34m#{self}\e[0m"
  when :light_blue  then "\e[1;34m#{self}\e[0m"
  when :purple      then "\e[35m#{self}\e[0m"
  when :pink        then "\e[1;35m#{self}\e[0m"
  when :cyan        then "\e[36m#{self}\e[0m"
  when :light_cyan  then "\e[1;36m#{self}\e[0m"
  end
end
dnl() click to toggle source
# File lib/flumtter/app/core/string.rb, line 65
def dnl
  "\n#{self}\n"
end
exact_size() click to toggle source
# File lib/flumtter/app/core/string.rb, line 57
def exact_size
  self.each_char.map{|c| c.bytesize == 1 ? 1 : 2}.reduce(0, &:+)
end
has_mb?() click to toggle source
# File lib/flumtter/app/core/string.rb, line 69
def has_mb?
  self.bytes do |b|
    return true if  (b & 0b10000000) != 0
  end
  false
end
ljust(length, padstr=nil, rstr="") click to toggle source
# File lib/flumtter/app/core/string.rb, line 77
def ljust(length, padstr=nil, rstr="")
  length -= (exact_size - 1) if has_mb?
  length -= rstr.exact_size
  text = padstr.nil? ? _ljust(length) : _ljust(length, padstr)
  text << rstr
end
Also aliased as: _ljust
max_char_of_lines() click to toggle source
# File lib/flumtter/app/core/string.rb, line 138
def max_char_of_lines
  max = self.each_line.max_by{|str|str.size}
  max.nil? ? 0 : max.size
end
nl(i=1) click to toggle source
# File lib/flumtter/app/core/string.rb, line 61
def nl(i=1)
  self + "\n"*i
end
nshift(n=2) click to toggle source
# File lib/flumtter/app/core/string.rb, line 103
def nshift(n=2)
  self.each_line.map.with_index do |line, i|
    if i.zero?
      line.chomp
    else
      " "*n + line.chomp
    end
  end.join("\n")
end
range?(max) click to toggle source
# File lib/flumtter/app/core/string.rb, line 128
def range?(max)
  unless 1 <= self.size && self.size <= max
    raise RangeError
  end
end
shift(n=2) click to toggle source
# File lib/flumtter/app/core/string.rb, line 97
def shift(n=2)
  self.each_line.map do |line|
    " "*n + line.chomp
  end.join("\n")
end
size_of_lines() click to toggle source
# File lib/flumtter/app/core/string.rb, line 134
def size_of_lines
  self.each_line.to_a.size
end
split_num(n) click to toggle source
# File lib/flumtter/app/core/string.rb, line 113
def split_num(n)
  text = []
  tmp = ""
  self.each_char.each do |char|
    if tmp.exact_size < n
      tmp << char
    else
      text << tmp
      tmp = char
    end
  end
  text << tmp
  text.join("\n")
end
terminal_title() click to toggle source
# File lib/flumtter/app/core/string.rb, line 6
def terminal_title
  print "\033];#{self}\007"
  at_exit{print "\033];\007"}
end
title() click to toggle source
# File lib/flumtter/app/core/string.rb, line 11
def title
  "【#{self}】"
end
to_camel() click to toggle source
# File lib/flumtter/app/core/string.rb, line 2
def to_camel
  self.split(/_/).map(&:capitalize).join
end
to_em() click to toggle source
# File lib/flumtter/app/core/string.rb, line 84
def to_em
  self.tr('0-9a-zA-Z', '0-9a-zA-Z')
end
to_en() click to toggle source
# File lib/flumtter/app/core/string.rb, line 88
def to_en
  self.tr('0-9a-zA-Z', '0-9a-zA-Z')
end
to_reg() click to toggle source
# File lib/flumtter/app/core/string.rb, line 92
def to_reg
  reg = Regexp.union(self, self.to_em)
  /^#{Regexp.new(reg.source, Regexp::IGNORECASE)}[ | ]*(.*)/
end