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
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
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) > endThis 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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---