Class: String
Instance Method Summary collapse
-
#/(o) ⇒ String
The original path concatenated with o.
-
#^(h = {}) ⇒ Object
Quick replacement of variables in a string with the hash equivalent Usage: “:god bless :country” ^ => “Budda”, :country => “India” #=> “Budda bless India”.
-
#camelcase ⇒ Object
Turn a downcased string and capitalize it so that it can be a class doc_river #=> DocRiver.
-
#classify ⇒ Object
Turn a string from lowercased with a .
-
#collect_each_line_with_index(&block) ⇒ Object
Collect every line in the the array of the string split by newlines and assign an index with the line on the enumeration.
-
#constantize(mod = Object) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string.
-
#dasherize ⇒ Object
“FooBar”.dasherize #=> “foo-bar”.
-
#json_parse ⇒ Object
Parse json string to a ruby object.
-
#keyerize ⇒ Object
Generate a unique integer key for this string.
-
#macify ⇒ Object
Take a mac address and split it to map to the arp -a response Just rip off the first 0 if the first char is 0.
-
#path_quote ⇒ Object
Substitute spaces in a path for ‘\ ’ spaces.
-
#pluralize(count = 2) ⇒ Object
dumb and ugly pluralize.
-
#safe_quote ⇒ Object
Strip quotes from the string and replaced them with backslashed quotes.
-
#sanitize ⇒ Object
Strip ugly characters out of a string.
-
#snake_case ⇒ Object
“FooBar”.snake_case #=> “foo_bar”.
-
#to_hash ⇒ Object
Read a new-line separated string and turn the string from the form a = “b” b = “c” into a hash => “b”, :b => “c”.
-
#top_level_class ⇒ Object
Get the top level class such as: Dr::Pepper #=> pepper.
Instance Method Details
#/(o) ⇒ String
Returns The original path concatenated with o.
132 133 134 |
# File 'lib/core/string.rb', line 132 def /(o) File.join(self, o.to_s) end |
#^(h = {}) ⇒ Object
Quick replacement of variables in a string with the hash equivalent Usage:
":god bless :country" ^ {:god => "Budda", :country => "India"} #=> "Budda bless India"
7 8 9 |
# File 'lib/core/string.rb', line 7 def ^(h={}) self.gsub(/:([\w]+)/) {h[$1.to_sym] if h.include?($1.to_sym)} end |
#camelcase ⇒ Object
Turn a downcased string and capitalize it so that it can be a class doc_river #=> DocRiver
45 46 47 |
# File 'lib/core/string.rb', line 45 def camelcase gsub(/(^|_|-)(.)/) { $2.upcase } end |
#classify ⇒ Object
Turn a string from lowercased with a . to a classified classname rice_and_beans #=> “RiceAndBeans” handles subclassed and namespaced classes as well for instance
rice::and::beans #=> Rice::And::Beans
65 66 67 |
# File 'lib/core/string.rb', line 65 def classify self.sub(/.*\./, '').split("::").map {|ele| ele.camelcase }.join("::") end |
#collect_each_line_with_index(&block) ⇒ Object
Collect every line in the the array of the string split by newlines and assign an index with the line on the enumeration
94 95 96 97 98 |
# File 'lib/core/string.rb', line 94 def collect_each_line_with_index(&block) returning [] do |arr| arr << self.split(/\n/).collect_with_index(&block) end.flatten end |
#constantize(mod = Object) ⇒ Object
Constantize tries to find a declared constant with the name specified in the string. It raises a NameError when the name is not in CamelCase or is not initialized.
Examples
"Module".constantize #=> Module
"Class".constantize #=> Class
83 84 85 86 87 88 89 90 |
# File 'lib/core/string.rb', line 83 def constantize(mod=Object) camelcased_word = classify begin mod.module_eval(camelcased_word, __FILE__, __LINE__) rescue NameError nil end end |
#dasherize ⇒ Object
“FooBar”.dasherize #=> “foo-bar”
55 56 57 |
# File 'lib/core/string.rb', line 55 def dasherize gsub(/\B[A-Z]+/, '-\&').downcase end |
#json_parse ⇒ Object
Parse json string to a ruby object
137 138 139 140 141 142 143 |
# File 'lib/core/string.rb', line 137 def json_parse return nil if self == "null" || self.empty? result = JSON.parse self if result.respond_to? :keys result.symbolize_keys! end end |
#keyerize ⇒ Object
Generate a unique integer key for this string
24 25 26 27 28 29 30 |
# File 'lib/core/string.rb', line 24 def keyerize signed_short = 0x7FFFFFFF len = self.sanitize.length hash = 0 len.times{ |i| hash = self[i] + ( hash << 6 ) + ( hash << 16 ) - hash } hash & signed_short end |
#macify ⇒ Object
Take a mac address and split it to map to the arp -a response Just rip off the first 0 if the first char is 0
121 122 123 |
# File 'lib/core/string.rb', line 121 def macify split(":").map {|a| a[0].chr == "0" ? a[1].chr : a}.join(":") end |
#path_quote ⇒ Object
Substitute spaces in a path for ‘\ ’ spaces
38 39 40 |
# File 'lib/core/string.rb', line 38 def path_quote self.safe_quote.gsub(/[ ]/, '\ ') end |
#pluralize(count = 2) ⇒ Object
dumb and ugly pluralize
146 147 148 |
# File 'lib/core/string.rb', line 146 def pluralize(count=2) count > 1 ? self + "s" : self end |
#safe_quote ⇒ Object
Strip quotes from the string and replaced them with backslashed quotes
33 34 35 |
# File 'lib/core/string.rb', line 33 def safe_quote self.gsub(/['"]/, '\\\"') end |
#sanitize ⇒ Object
Strip ugly characters out of a string
19 20 21 |
# File 'lib/core/string.rb', line 19 def sanitize self.gsub(/[ \.\/\-]*/, '') end |
#snake_case ⇒ Object
“FooBar”.snake_case #=> “foo_bar”
50 51 52 |
# File 'lib/core/string.rb', line 50 def snake_case gsub(/\B[A-Z]+/, '_\&').downcase end |
#to_hash ⇒ Object
Read a new-line separated string and turn the string from the form
a = "b"
b = "c"
into a hash
{:a => "b", :b => "c"}
106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/core/string.rb', line 106 def to_hash split("\n").inject({}) do |sum,line| if line.include?("=") l = line.split("=").map {|a| a.strip } key = l[0].to_sym value = l[1].gsub(/\"/, '') sum.merge(key => value) else sum end end end |
#top_level_class ⇒ Object
Get the top level class such as:
Dr::Pepper #=> pepper
14 15 16 |
# File 'lib/core/string.rb', line 14 def top_level_class self.classify.split("::").last.snake_case end |