Skip to content

Instantly share code, notes, and snippets.

@takahashilabo
Last active December 16, 2015 09:09
Show Gist options
  • Save takahashilabo/5411365 to your computer and use it in GitHub Desktop.
Save takahashilabo/5411365 to your computer and use it in GitHub Desktop.
count the number of words in a string
class String
def count_the_number_of_words_in_a_string(word, pos = 0, num = 0)
pos = self.index(word, pos)
return num if pos == nil
count_the_number_of_words_in_a_string(word, pos + 1, num + 1)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment