Class: String
- Inherits:
-
Object
- Object
- String
- Defined in:
- lib/standard/string.rb
Instance Method Summary collapse
-
#bold(color = nil) ⇒ Object
Make a string bold and colorful, with colors a user can understand.
-
#clean ⇒ Object
Remove all tags inside of a string.
-
#color(color = nil) ⇒ Object
Same as bold, but just make a string colorful.
-
#indent(indent) ⇒ Object
indent a string and all lines that belong to it.
-
#pad(c, s = " ") ⇒ Object
Trim or expand a string to a certain length.
-
#parse ⇒ Object
Make json parsing a lot easier and less to write.
- #to_ascii ⇒ Object
-
#to_bool ⇒ Object
It is just convenient to have a custom to_bool function.
- #url_encode ⇒ Object
Instance Method Details
#bold(color = nil) ⇒ Object
Make a string bold and colorful, with colors a user can understand. Not those cryptic numbers.
50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/standard/string.rb', line 50 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 ⇒ Object
Remove all tags inside of a string.
35 36 37 |
# File 'lib/standard/string.rb', line 35 def clean self.gsub(/<.+?>/,"") end |
#color(color = nil) ⇒ Object
Same as bold, but just make a string colorful.
64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/standard/string.rb', line 64 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
78 79 80 81 |
# File 'lib/standard/string.rb', line 78 def indent(indent) s = self.split("#$/") " "*indent+s.join("#$/"+" "*indent) end |
#pad(c, s = " ") ⇒ Object
Trim or expand a string to a certain length.
39 40 41 42 43 44 45 46 47 |
# File 'lib/standard/string.rb', line 39 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 ⇒ Object
Make json parsing a lot easier and less to write.
25 26 27 |
# File 'lib/standard/string.rb', line 25 def parse JSON.parser.new(self).parse end |
#to_ascii ⇒ Object
28 29 30 |
# File 'lib/standard/string.rb', line 28 def to_ascii self.gsub("_"," ").encode("us-ascii", :invalid => :replace, :undef => :replace, :replace => "") end |
#to_bool ⇒ Object
It is just convenient to have a custom to_bool function. Only “true” gets turned into true, anything else is false.
84 85 86 87 88 89 90 |
# File 'lib/standard/string.rb', line 84 def to_bool if self.downcase == "true" then true else false end end |
#url_encode ⇒ Object
31 32 33 |
# File 'lib/standard/string.rb', line 31 def url_encode CGI.escape(self) end |