Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/string/color.rb,
lib/string/float.rb,
lib/string/width.rb,
lib/string/integer.rb,
lib/string/mismatch.rb,
lib/string/escapezsh.rb,
lib/string/splitparens.rb

Constant Summary collapse

COLOR_CODE =
{
  :black   => 0, :red     => 1, :green   => 2, :yellow  => 3,
  :blue    => 4, :magenta => 5, :cyan    => 6, :white   => 7,
  :default => 9,
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.float?(str) ⇒ Boolean

Return true if the 'str' express float, else return false.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
# File 'lib/string/float.rb', line 8

def self.float?(str)
  begin
    Float(str)
    true
  rescue ArgumentError
    false
  end
end

.integer?(str) ⇒ Boolean

Return true if the 'str' express integer, else return false.

Returns:

  • (Boolean)


8
9
10
11
12
13
14
15
# File 'lib/string/integer.rb', line 8

def self.integer?(str)
  begin
    Integer(str)
    true
  rescue ArgumentError
    false
  end
end

Instance Method Details

#close_paren?Boolean

Return true if self is a single character and close parenthesis.

Returns:

  • (Boolean)


51
52
53
# File 'lib/string/splitparens.rb', line 51

def close_paren?
  ')}]>)}]>〕】〉》」』'.split('').include?(self)
end

#color(fg, bg = :default) ⇒ Object

Add escape characters at head and tail of self. Foreground and background can be indicated by argued symbols. 0: black 1: red 2: green 3: yellow 4: blue 5: magenta 6: cyan 7: white 9: default Argument bg indicates background color. If not indicated, default color. Argument fg indicates foreground color. This must be indicated.



24
25
26
# File 'lib/string/color.rb', line 24

def color( fg, bg = :default )
  "\e[3#{COLOR_CODE[ fg ]};4#{COLOR_CODE[ bg ]}m" + self + "\e[39;49m"
end

#escape_zshObject

Add backslash escape at the characters that have perticular meaning in zsh.



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/string/escapezsh.rb', line 6

def escape_zsh
  temp = dup
  temp.gsub!('\\', '\\\\\\') # This must be at first, not to substitute other results.
  #temp.gsub!('/', '/') # / is not for escape, due to directory separator.
  temp.gsub!(' ', '\ ') ; temp.gsub!('!', '\!') ; temp.gsub!('"', '\"') ;
  temp.gsub!('#', '\#') ; temp.gsub!('$', '\$') ; temp.gsub!('%', '\%') ;
  temp.gsub!(')', '\)') ; temp.gsub!('(', '\(') ; temp.gsub!('*', '\*') ;
  temp.gsub!(',', '\,') ; #temp.gsub!('-', '\-') ; #temp.gsub!('.', '\.') ;
  temp.gsub!(':', '\:') ; temp.gsub!(';', '\;') ; temp.gsub!('<', '\<') ;
  temp.gsub!('=', '\=') ; temp.gsub!('>', '\>') ; temp.gsub!('?', '\?') ;
  temp.gsub!('@', '\@') ; temp.gsub!('[', '\[') ; temp.gsub!(']', '\]') ;
  temp.gsub!('^', '\^') ; temp.gsub!('_', '\_') ; temp.gsub!('{', '\{') ;
  temp.gsub!('|', '\|') ; temp.gsub!('}', '\}') ; temp.gsub!('~', '\~') ;
  temp.gsub!('`', '\\\`') ; temp.gsub!('&', '\\\&') ;
  temp.gsub!('+', '\\\+') ; temp.gsub!("'", "\\\\'") ;
  return temp
end

#float?Boolean

Return true if the 'str' express float, else return false.

Returns:

  • (Boolean)


18
19
20
# File 'lib/string/float.rb', line 18

def float?
  self.class.float?(self)
end

#integer?Boolean

Return true if the 'str' express integer, else return false.

Returns:

  • (Boolean)


18
19
20
# File 'lib/string/integer.rb', line 18

def integer?
  self.class.integer?(self)
end

#mismatch(other) ⇒ Object

Return the first index that meet mismatch between self and other String. Return nil if self == other.

Note that this method may return an index more than self.size. e.g., "abc".mismatch( "abcd" ) #=> 3



11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/string/mismatch.rb', line 11

def mismatch( other )
  ary1 = self.split( // )
  ary2 = other.split( // )
  index = nil
  [ ary1.size, ary2.size ].max.times do |i|
    if ( ary1[i] != ary2[i] )
      index = i
      break
    end
  end
  index
end

#open_paren?Boolean

Return true if self is a single character and open parenthesis.

Returns:

  • (Boolean)


46
47
48
# File 'lib/string/splitparens.rb', line 46

def open_paren?
  '({[<({[<〔【〈《「『'.split('').include?(self)
end

#split_parensObject

Split String using correspond parentheses. "(ab)(cd(ef))gh(ij)kl(mn)" -> ["(ab)", "(cd(ef))", "gh", "(ij)", "kl", "(mn)"] "((ab)(cd(ef))gh(ij)kl(mn)" -> ["((ab)(cd(ef))gh(ij)kl(mn)"] "(ab)(cd(ef)))gh(ij)kl(mn)" -> ["(ab)", "(cd(ef))", ")gh(ij)kl(mn)"]



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/string/splitparens.rb', line 13

def split_parens
  depth = 0
  current = ''
  results = []
  self.split('').each do |char|
    if char.open_paren?
      if (depth == 0 )
        results << current
        current = ''
      end
      current << char
      depth += 1
      next
    end

    if char.close_paren?
      depth -= 1
      current << char
      if (depth == 0)
        results << current
        current = ''
      end
      next
    end

    current << char
  end
  results << current
  results.delete_if{|item| item.empty? }
  return results
end

#widthObject

Return character width in rxvt-unicode.



6
7
8
9
10
11
# File 'lib/string/width.rb', line 6

def width
  chars = ' -~' + '’”' # width of one alphabetic char.
  num_ascii = count(chars)
  num_wide = size - num_ascii
  return 2 * num_wide + num_ascii
end