Skip to content

Instantly share code, notes, and snippets.

@maxjacobson
Last active December 13, 2015 21:38
Show Gist options
  • Save maxjacobson/4978111 to your computer and use it in GitHub Desktop.
Save maxjacobson/4978111 to your computer and use it in GitHub Desktop.
class String
def word_count
self.split(" ").length
end
def char_count
self.chars.to_a.length
end
def contains(word)
self.gsub(/[^\w]/, " ").split(" ").count(word)
end
end
sentence = "I really, really, really love it when my dog licks my cat."
puts sentence.word_count
puts sentence.char_count
puts sentence.contains("really")
puts sentence.contains("dog")
puts sentence.contains("codecrew")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment