--------------030409040603080300060309 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 25.08.2006 07:59, Graham Wideman wrote: > Folks: > > I'm new to Ruby, though not to programming in general. So I'm looking around > for some of the mechanisms I'm used to finding, and one of them is an > apparatus for Collections (as for example used in C++, OP, VB etc). > > I'm hoping that someone can point me in the right direction as to where this > functionality can be found, or failing that suggest the most advantageous > starting point to create it based on more primitive classes. > > Main features: > > 1. The contained objects are user-defined types having multiple fields (data > members). This in itself seems no problem for Ruby. > > 2. Order/Retrieve by index. The collection should stay ordered by insertion > order and support retrieval by integer index. (Sorted collection is a > separate issue, of course.) > > 3. Insert/Append/Remove/Delete. (List behavior) Allow appending (at end) or > inserting at arbitrary location, and removal or deletion of arbitrary > members. > > 4. Find by key: (Dictionary behavior etc) Ability to retrieve member objects > by supplying a value to match against one of the object's fields. Often this > is simple a Name field on each object, which functions as a key, but at > other times one might want lookup based on some other field or fields. > > So far I've read plenty that seems related: arrays, hashes, Enumerable, and > several related chapters in PickAxe and Ruby for Rails. Although > "collections" are mentioned in many of these sources, I've not yet hit on a > treatment of the ruby way for the Collection basics described above (eg: R > on R's Collection chapter's description of "insert" is simply replacing the > Nth element of an array). > > Needless to say, I'd much appreciate comments illuminating this area! For the sake of discussion I present a different approach. As long as you don't need 4 (lookup by key) an Array is perfect. However, with the lookup IMHO a new data structure is preferred because then consistency can be handled internally. I'll attach a half grown quick hack to demonstrate what I mean. Kind regards robert --------------030409040603080300060309 Content-Type: text/plain; name dx-coll.rb" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename dx-coll.rb" # untested and incomplete sample require 'set' class IndexedCollection include Enumerable def initialize(enum ) @data ] @indexes } add_all enum end def add_index(field) field ield.to_sym idx indexes[field] ash.new {|h,k| h[k] et.new} @data.each do |e| idx[e.send(field)] << e end self end def lookup(field, value) field ield.to_sym idx indexes[field] or raise ArgumentError, "No such index: #{field}" idx[value].to_a end def add_all(enum) enum.each {|e| self << e} self end def <<(e) @data << e @indexes.each do |field, index| index[e.send(field)] << e end self end def each(&b) @data.each(&b) self end def size() @data.size end end --------------030409040603080300060309--