I'm dealing with a file that has a bunch of entries like this
2012-07-15 10:16:27 C ?\path\to a filename\ called this file.doc
I want to take a line like this and cut the first 3 fields separated by spaces. So...
var1 = 2012-07-15
var2 = 10:16:27
var3 = c
I've googled around and I just can not seem to find the right method to use. Thank you for your help!
Ruby's String#split
accepts a limit as its second parameter. This will do exactly what you're looking for:
irb(main):005:0> str = "2012-07-15 10:16:27 C ?\path\to a filename\ called this file.doc"
=> "2012-07-15 10:16:27 C ?path\to a filename called this file.doc"
irb(main):006:0> str.split " ", 4
=> ["2012-07-15", "10:16:27", "C", "?path\to a filename called this file.doc"]
You can use destructuring to assign these into local variables, if you want:
one, two, three, rest = str.split " ", 4