thestateofmay thestateofmay - 4 years ago 118
Ruby Question

Parsing a dataset in Ruby

I'm trying to read a dataset and parse that into the data I need. The file will consist of lines of strings like this:

id: 1234567 synset: test,exam


I want to then obtain the
id
number and the
synset
word. So in this case, I want
1234567
and
test,exam


Here's what I've come up with, but I'm sure there are better ways.

File.open(synsets_file, "r") do |f|
f.each_line do |line|
id = line.split[1].to_i
nouns = line.split[3]
#do things with id and nouns
end
end

Answer Source

Your example is fine. You could use split once with this syntax :

File.foreach(synsets_file) do |line|
  _, id, _, nouns = line.chomp.split(/\s+/, 4)
  # do things with id and nouns
end

Using 4 as second parameter for split will ensure that nouns isn't split if there are spaces inside.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download