Hello Im trying to write a a ruby program that takes a list of books
title ,author so on and i can read in the file with no problem but im
trying to store the list into either a array or hash so i ca say run
different methods view, delete ect…
anyhelp would be great this is what i have so far
IO.foreach(“book_list.txt”) do |line|
catalog=line.chomp.split(",")
products.store(items[0].to_i,
[catalog[1],catalog[2].to_i,catalog[3].to_i)
end
thanks for anyhelp
Gabriel
GabrielG1976 wrote:
IO.foreach(“book_list.txt”) do |line|
catalog=line.chomp.split(",")
products.store(items[0].to_i,
[catalog[1],catalog[2].to_i,catalog[3].to_i)
end
This group discusses Rails, not raw Ruby. You’d get better answers on
the ruby
newsgroup/mailing list/thing.
And I would just use YAML::load to read the file, and let YAML do all
that
low-level parsing and representing.
This is how you can do it:
ar = []
ha = {}
IO.foreach(“book_list.txt”) do |line|
catalog=line.chomp.split(",")
products.store(items[0].to_i,
[catalog[1],catalog[2].to_i,catalog[3].to_i)
To array
ar << catalog
To Hash
ha[‘some_key’] = catalog
end
It can be done as oneliner:
ar=[]
File.readlines(“book_list.txt”).each {|line| ar <<
line.chomp.split(",")}
by
TheR