Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/standard/string.rb

Instance Method Summary collapse

Instance Method Details

#bold(color = nil) ⇒ Object

Make a string bold and colorful, with colors a user can understand. Not those cryptic numbers.



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/standard/string.rb', line 46

def bold(color=nil)
  c = case color
  when "black"  then  "\e[30;1m"
  when "red"    then  "\e[31;1m"
  when "green"  then  "\e[32;1m"
  when "yellow" then  "\e[33;1m"
  when "blue"   then  "\e[34;1m"
  when "purple" then  "\e[35;1m"
  when "cyan"   then  "\e[36;1m"
  else                "\e[37;1m"
  end
  c+self+"\e[0m"
end

#cleanObject

Remove all tags inside of a string.



31
32
33
# File 'lib/standard/string.rb', line 31

def clean
  self.gsub(/<.+?>/,"")
end

#color(color = nil) ⇒ Object

Same as bold, but just make a string colorful.



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/standard/string.rb', line 60

def color(color=nil)
  c = case color
  when "black"  then  "\e[30;0m"
  when "red"    then  "\e[31;0m"
  when "green"  then  "\e[32;0m"
  when "yellow" then  "\e[33;0m"
  when "blue"   then  "\e[34;0m"
  when "purple" then  "\e[35;0m"
  when "cyan"   then  "\e[36;0m"
  else                "\e[37;0m"
  end
  c+self+"\e[0m"
end

#indent(indent) ⇒ Object

indent a string and all lines that belong to it



74
75
76
77
# File 'lib/standard/string.rb', line 74

def indent(indent)
  s = self.split("#$/")
  " "*indent+s.join("#$/"+" "*indent)
end

#pad(c, s = " ") ⇒ Object

Trim or expand a string to a certain length.



35
36
37
38
39
40
41
42
43
# File 'lib/standard/string.rb', line 35

def pad(c,s=" ")
  if self.length > c then
    self[0,c-3]+"..."
  elsif self.length < c then
    self+s.to_s*(c-self.length)
  else
    self
  end
end

#parseObject

Make json parsing a lot easier and less to write.



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

def parse
  JSON.parser.new(self).parse
end

#to_asciiObject



27
28
29
# File 'lib/standard/string.rb', line 27

def to_ascii
  self.gsub("_"," ").encode("us-ascii", :invalid => :replace, :undef => :replace, :replace => "")
end

#to_boolObject

It is just convenient to have a custom to_bool function. Only “true” gets turned into true, anything else is false.



80
81
82
83
84
85
86
# File 'lib/standard/string.rb', line 80

def to_bool
  if self.downcase == "true" then
    true
  else
    false
  end
end