Created
January 14, 2011 21:50
-
-
Save wbailey/780326 to your computer and use it in GitHub Desktop.
Using a class to define methods that examine the contents of a string
This file contains 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 MyString | |
attr_accessor :str | |
def initialize str | |
self.str = str | |
end | |
def consonants | |
str.gsub /[aeiuo0-9]/i, '' | |
end | |
def vowels | |
str.gsub /[^aieou]/i, '' | |
end | |
def capitols | |
str.gsub /[^A-Z]/, '' | |
end | |
def lowercase | |
str.gsub /[^a-z]/, '' | |
end | |
def numbers | |
str.gsub /[^0-9]/, '' | |
end | |
end | |
puts str = "f2fp2LOCBVXBn2ifcgpy" | |
mystr = MyString.new str | |
%w{consonants vowels capitols lowercase numbers}.each {|m| puts "#{m}: #{mystr.send m}"} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment