From: Niklas Frykholm Date: 2001-08-08T05:59:50+09:00 Subject: [ruby-talk:19309] Re: Virtual list > For starts, I'm new to Ruby still, forgive me naivette. (Is that even a > word?) I'm mostly a C++ programmer, so my terminology might be a little off > towards that direction.. ^,^ > > If I have a class Object (in C++, called AMObject in Ruby), and it has two > lists in it - a list of attributes (properties of an item), and containers > (difference types of containers that store more Objects), how can I make a > clean interface to traversing these objects in Ruby? > > In C++ the tree is like: > Object (class object) > attributes (class list) > obj_attr > obj_attr > ... > containers (class list) > container > container > ... > > Something like this in Ruby would be nice: > obj.attrs.for_each { |a| whatever... } > How do I go about making a clean class for "attrs?" I am not entirely sure I understand you, but to me it seems as if you are still thinking in "static typing". Remember that Ruby is dynamically typed, each object knows its own type, so you don't need to specify it. So to create a list of objects you would just put them in a Ruby array, no matter what their type was. attr = [attr1, attr2, attr3, ...] And then you could indeed do attr.each {..} or use any of the other methods for ruby arrays. As for the containers, put them in an array as well, and let each of the containers define "each" and include Enumerable. Then you can use all the containers in the same way. (But you may not even need different containers, since Array can hold objects of any type, it is good for most things.) If you are used to static typing you may very well think that this seems "unclean", but as you get more used to dynamic typing you will worry less and less about that. // Niklas