class String
Public Instance Methods
bold(color=nil)
click to toggle source
Make a string bold and colorful, with colors a user can understand. Not those cryptic numbers.
# 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
clean()
click to toggle source
Remove all tags inside of a string.
# File lib/standard/string.rb, line 31 def clean self.gsub(/<.+?>/,"") end
color(color=nil)
click to toggle source
Same as bold, but just make a string colorful.
# 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)
click to toggle source
indent a string and all lines that belong to it
# File lib/standard/string.rb, line 74 def indent(indent) s = self.split("#$/") " "*indent+s.join("#$/"+" "*indent) end
pad(c,s=" ")
click to toggle source
Trim or expand a string to a certain length.
# 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
parse()
click to toggle source
Make json parsing a lot easier and less to write.
# File lib/standard/string.rb, line 24 def parse JSON.parser.new(self).parse end
to_ascii()
click to toggle source
# File lib/standard/string.rb, line 27 def to_ascii self.gsub("_"," ").encode("us-ascii", :invalid => :replace, :undef => :replace, :replace => "") end
to_bool()
click to toggle source
It is just convenient to have a custom to_bool
function. Only “true” gets turned into true, anything else is false.
# File lib/standard/string.rb, line 80 def to_bool if self.downcase == "true" then true else false end end