Chop files of IO separators?

I am trying to parse a text file containing a variable number of words and numbers per line, for example:

foo 4.500 bar 3.00
1.3 3 foo bar

How can I read a file limited to spaces instead of newlines? Is there a way that I can set a method File("file.txt").foreachto use spaces instead of newlines as a separator?

+5
source share
3 answers

you can use

open('file.txt').read.split.each

to give you an iterator over space (or newline) separated words.

+3
source

File.open ("file.txt"). read.split (""). each

+3
source

slurp, .

IO.foreach. char char:

File.foreach(filename," ") {|string| puts string}

, "this is an example",

"this"
"is"
"an"
"example"
+3
source

All Articles