Skip to content

Instantly share code, notes, and snippets.

@wbailey
Created January 14, 2011 21:50
Show Gist options
  • Save wbailey/780326 to your computer and use it in GitHub Desktop.
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
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