Skip to content

Instantly share code, notes, and snippets.

@metallurgix
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save metallurgix/ce99ae20b7ec618e7d80 to your computer and use it in GitHub Desktop.

Select an option

Save metallurgix/ce99ae20b7ec618e7d80 to your computer and use it in GitHub Desktop.
Useful String Methods
class String
#Capitalize the beginning of ever word in a string.
def word_capitalize
self.gsub(/\b\w/){$&.upcase}
end
#Check for balanced parantheses
def para_check?
para=Array.new
self.each_char do |chr|
case chr
when "("
para<<chr
when "{"
para<<chr
when "["
para<<chr
when ")"
if !(para.pop=="(")
return false
end
when "}"
if !(para.pop=="{")
return false
end
when "]"
if !(para.pop=="[")
return false
end
end
end
if !para.empty?
return false
else
return true
end
end
#Remove duplicate chars
def rem_dup
self.each_char do |c|
if self.count(c)>1
self.gsub!(c,"")
end
end
return self
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment