Class: String

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

Instance Method Summary collapse

Instance Method Details

#/(o) ⇒ String

Returns The original path concatenated with o.

Examples:

"merb"/"core_ext" #=> "merb/core_ext"

Parameters:

  • o (String)

    The path component to join with the string.

Returns:

  • (String)

    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

#camelcaseObject

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

#classifyObject

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

#dasherizeObject

“FooBar”.dasherize #=> “foo-bar”



55
56
57
# File 'lib/core/string.rb', line 55

def dasherize
  gsub(/\B[A-Z]+/, '-\&').downcase
end

#json_parseObject

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

#keyerizeObject

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

#macifyObject

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_quoteObject

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_quoteObject

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

#sanitizeObject

Strip ugly characters out of a string



19
20
21
# File 'lib/core/string.rb', line 19

def sanitize
  self.gsub(/[ \.\/\-]*/, '')
end

#snake_caseObject

“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_hashObject

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_classObject

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