Topic: How to use a class as a temporary table of records in memory

I want a list of court availability times with the following fields.
venue_id, start_time, end_time, just like a table of records.

I want to create new records on a loop and then iterate over the records. What is the best way to do this? It would seem easy if I used the db to store the records but I don't need persistence.

myClass.each do produces a no method error. If I inspect @availability_grid I get only one record, there should be many.

Here is my code so far.

class VenueAvailabilityGrid

  attr_accessor :venue_id, :start_time, :end_time, :team_a_played, :team_b_played, :venue_played

  def initialize(venue_id, start_time, end_time, team_a_played, team_b_played, venue_played)
    @venue_id, @start_time, @end_time, @team_a_played, @team_b_played, @venue_played =
           venue_id, start_time, end_time, team_a_played, team_b_played, venue_played
  end

end

@availability_grid = VenueAvailabilityGrid.new(venue_id, start_time, end_time, team_a_played, team_b_played, venue_played)

Re: How to use a class as a temporary table of records in memory

Why not generate (new) records and store them in an array (without saving them to the DB)?  On the array you can use whatever iterator feels natural.

Re: How to use a class as a temporary table of records in memory

I'd love to if I could only figure out how....

I can't find any code which shows me how to do that. I created a class but it only seems to hold one record. I figure the best way is to try to append class instances to an array.

What I need is a code example of how to dynamically create an array of class instances or an array of hashes and then loop back through the array an extract values from the hash. Amazingly, its very hard to find any explanation or tutorials about how this is done.

Re: How to use a class as a temporary table of records in memory

One very simple example for hashes:

my_array = []
(1..5).each {|x| my_array << {:a=>x, :b => x*x}}

my_array.each do |element|
  puts element[:a]
end

(There are more compact ways to do this; it is for explanatory purposes.)  The << operator will push (add, append, whatever you call it) an element to an array (supposing the left side operand is an array).

And one for AR classes:

my_array = []
(1..5).each {|x| my_array << MyModel.new}

my_array.each do |record|
  puts record.attribute
end

where 'MyModel' is a model of yours and 'attribute' is one of its attributes (columns).

Hope it helps.  Ask more if not.  The ruby docs for hashes and arrays are well worth reading: http://ruby-doc.org/core/classes/Hash.html and http://ruby-doc.org/core/classes/Array.html

Re: How to use a class as a temporary table of records in memory

I have a recurring need to be able to do a complex query (which returns
   relativley few records 5-40), get that
   recordset and be able to change (edit or modify) it in memory. And then do
   something with the changed records. I don't really want to tie the records
   back to their source, but, say, insert them into some other table.

Sounds to me as if you need to do a little reading on Data Access Objects (DAO)
programming. If you don't have it, get the  _Access 97 Developer's Handbook_,
by Litwin, Getz, and Gilbert, published by SYBEX. It has good coverage of this
subject. Depending on the details of what you want to do, you may or may not
need to use an array of User Defined Types (UDT) in the process.


_____________________
Overseas consultants in India

Last edited by adamricky (2011-08-04 03:37:24)