Class: String

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

Instance Method Summary collapse

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


199
200
201
# File 'lib/useful_class_extensions.rb', line 199

def blank?
  return self.empty? || self.nil?
end

#camelize_homebrew(lower_case_and_underscored_word, first_letter_in_uppercase = true) ⇒ Object



169
170
171
172
173
# File 'lib/useful_class_extensions.rb', line 169

def camelize_homebrew(lower_case_and_underscored_word, first_letter_in_uppercase = true)
  if first_letter_in_uppercase
    lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
  end
end

#classify_homebrewObject



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/useful_class_extensions.rb', line 157

def classify_homebrew
  if self.split(//).last == "s"
    if self.split(//)[self.split(//).length-3..self.split(//).length].join == "ies"
      camelize_homebrew(self.split(//)[0..self.split(//).length-4].join("")+"y")
    else
      camelize_homebrew(self.sub(/.*\./, '').chop)
    end
  else
    camelize_homebrew(self.sub(/.*\./, ''))
  end
end

#constantizeObject



175
176
177
# File 'lib/useful_class_extensions.rb', line 175

def constantize
  return Object.const_defined?(self) ? Object.const_get(self) : Object.const_missing(self)
end

#pluralizeObject



148
149
150
# File 'lib/useful_class_extensions.rb', line 148

def pluralize
  self.underscore.concat("s")
end

#sanitize_for_streamingObject



152
153
154
155
# File 'lib/useful_class_extensions.rb', line 152

def sanitize_for_streaming
  # return self.split("").reject {|c| c.match(/[\w\'\-]/).nil?}.to_s
  return self.gsub(/[\'\"]/, '').gsub("#", "%23").gsub(' ', '%20')
end

#super_split(split_char) ⇒ Object



194
195
196
197
# File 'lib/useful_class_extensions.rb', line 194

def super_split(split_char)
  #This regexp is used in place of \W to allow for # and @ signs.
  return self.gsub(/[!$%\*\;{}\[\]\(\)\+=\'\"\|<>,~`]/, " ").split(split_char)
end

#super_stripObject



183
184
185
186
187
188
189
190
191
192
# File 'lib/useful_class_extensions.rb', line 183

def super_strip
  #This regexp is used in place of \W to allow for # and @ signs.
   if self.include?("#") || self.include?("@")
     return self
   elsif self.include?("http")
     return self
   else
     return self.strip.downcase.gsub(/[!$%\*&:.\;{}\[\]\(\)\-\_+=\'\"\|<>,\/?~`]/, "")
   end
end

#to_classObject



179
180
181
# File 'lib/useful_class_extensions.rb', line 179

def to_class
  return self.classify.constantize
end

#underscoreObject



144
145
146
# File 'lib/useful_class_extensions.rb', line 144

def underscore
  self.gsub(/::/, '/').gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2').tr("-", "_").downcase
end

#write(str) ⇒ Object



140
141
142
# File 'lib/useful_class_extensions.rb', line 140

def write(str)
  self << str
end