It turns out that I need to know how many words a text file has, I have already opened it correctly and tried the method length
, but it gives me the number of lines in the file.
I tried to make a gsub
to the variable that stores the file but it throws me an error.
I thought I'd try count
, but I'm still seeing how it's done.
The complicated thing is how you identify a word, as it can vary depending on your object, language, context, etc. A simple (but not perfect) way can be: a word is all text that is between two spaces (space, line break, tab, etc.) .
For the above definition then you could do something like this:
Example file (test.txt):
reject(&:empty?)
.Step-by-step execution: