Last active
August 29, 2015 14:04
-
-
Save metallurgix/ce99ae20b7ec618e7d80 to your computer and use it in GitHub Desktop.
Useful String Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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