Interacting With Class Objects in Ruby

Posted by michaelmichael on Stack Overflow See other posts from Stack Overflow or by michaelmichael
Published on 2010-04-21T15:16:10Z Indexed on 2010/04/21 15:33 UTC
Read the original article Hit count: 226

Filed under:
|

How can I interact with objects I've created based on their given attributes in Ruby?

To give some context, I'm parsing a text file that might have several hundred entries like the following:

ASIN: B00137RNIQ

-------------------------Status Info-------------------------
Upload created: 2010-04-09 09:33:45
Upload state: Imported
Upload state id: 3

I can parse the above with regular expressions and use the data to create new objects in a "Product" class:

class Product
  attr_reader :asin, :creation_date, :upload_state, :upload_state_id

  def initialize(asin, creation_date, upload_state, upload_state_id)
    @asin = asin 
    @creation_date = creation_date
    @upload_state = upload_state
    @upload_state_id = upload_state_id
  end
end

After parsing, the raw text from above will be stored in an object that look like this:

[#<Product:0x00000101006ef8 @asin="B00137RNIQ", @creation_date="2010-04-09 09:33:45  ", @upload_state="Imported  ", @upload_state_id="3">]

How can I then interact with the newly created class objects? For example, how might I pull all the creation dates for objects with an upload_state_id of 3? I get the feeling I'm going to have to write class methods, but I'm a bit stuck on where to start.

© Stack Overflow or respective owner

Related posts about ruby

Related posts about beginner