Q:

Keeping track of Objects created in Ruby

I have a class called Orders.  I want to keep an array of all the "Orders" instances so I can .each map/reduce/or otherwise query them.
Later I would like to Marshal "dump" and "load" the "Orders" array of objects.
My plan is to add each new object created onto an @@all_orders array stack on initialization.  Then I can do my .each method on the @@all_orders array, looping through all "orders" objects.
Am I approaching this the right OOP way? (code snippet to get the idea)...
class Orders

  @@all_orders = Array.new
  attr_accessor :order_no, :customer

  def initialize(order_no, customer)
    @id, @customer = order_no, customer
    @order_lines = Array.new
    @@all_orders << self
  end

A:

The proper Object-Oriented way to do this is to have a container you're putting the orders into. This can be a plain-old array or a special container class.
The big problem here is you don't have a proper context for storing the "all orders" data. All orders relevant to what? The entire application? If that's the case you need the concept of an order book or a database to store them.
For example:
class OrderBook < Array
end

order_book = OrderBook.new
order_book << Order.new(...)

Note that I've renamed Orders to Order as that's more accurate, singular name.
Auto-magically adding instances to a container is almost always a recipe for disaster. That sort of thing should be handled using a design pattern like model-controller. It's a huge assumption that you'd want to include it in the global orders pool.

