What is the Ruby idiom for reading input word-by-word? In other words
– how to process input while skipping all of the whitespace.
I would use this code in C++:
stream >> str;
Or something like this in C:
char str; // please no flame on fixed buffer size
I have tried to use Ruby’s scanf("%s") but found it severely broken
(as per my task)–it discards the rest of the input up to the
newline. I currently use the following approach, which I find ugly:
while not $stdin.eof? do
words = $stdin.gets().scan(/[^\s]+/)
words.each do |w|
It takes me to write an inner loop and reads the entire string into
memory and then splits it into array of words… Duh!
My application is not in any case a time- or memory-critical, nor did
I measured to find the performance bottleneck… however, I desire for
Please show me the Ruby way!
PS: Yes, I’ve searched the web, tutorials, FAQs, cookbooks, etc.
before posting this. No luck.