I can do bit of coding in ruby. I just touched objects and I am not so object literate, I mean I do not think in objects yet 🙂
I have data that I scrape from the forum on regular basis. I need fields like
- author, date posted, title, category, number of views, etc etc = array in my point of view.
Then I want to be able to these in ruby
- save the whole lot (quick solution is csv or xml – later probably some sql database)
- sort it by field
- load/read my file to update fields and do some statistics, extract some data
- add new fields easily in case I need to
edit, modify my “file/database” outside ruby.
I believe that I can do every operation like change the number of views of post, change the date of the last reply in the post etc etc either using array or object.
so my Question is: would you use
……………………………….. custom class/object or array?
could you tell why?
It would seem logical to me, at least, to make an object for storing and working with the data that you’re scraping. Typically, you’d have instance variables for each of the fields that you have mentioned (author, title, category, views, date_posted) and probably some methods to populate them from the scraped data as well as read/write them.
In terms of storing the data for these objects, using an ORM such as ActiveRecord or DataMapper makes this very easy. An ORM let’s you map the data in a data store, such as MySQL, to the corresponding Ruby objects. It will also provide a bunch of convenience methods for saving, updating and querying those objects.
However, it might be a good learning experience to try writing your own methods to map the data to XML files.