I have data of the form:
Manju She is a good girl
Raja He is okay boy
Manju She comes late in the class
Raja He is punctual
I want to create such a structure from this:
manju_hash = {"Manju1" => ["She", "is", "a","good","girl"], "Manju2" => ["She", "comes","late", "in", "the","class"]}
raja_hash = {"Raja1" => ["He", "is", "okay", "boy"], "Raja2" => ["He", "is", "punctual"]}
This is the code that I've written:
manju_hash = Hash.new
raja_hash = Hash.new
data_lines.split(/\n+/).each do |val|
value =val.split(/\s+/)
key = value[0]
if (key=='Manju')
value.delete(key)
manju_hash.merge!(key+(manju_hash.size+1).to_s => value)
elsif (key=='Raja')
value.delete(key)
raja_hash.merge!(key+(raja_hash.size+1).to_s => value)
end
end
Is this the right way to achieve this, or there is some other idiomatic ruby way? Some other improvement that I could do