From: Dave Aronson Date: 2011-11-25T03:36:02+09:00 Subject: Re: Genric List in Ruby On Thu, Nov 24, 2011 at 04:29, Sagar Varule wrote: > Class Thing >  string name; >  string id; > end > > Class Shelf >  List lstthing = new List; > end > > Above is C# code; How can I write same in Ruby.... It seems to me that the previous answers were essentially "Ruby lists *are* generic, what's wrong?". However, I interpret your question as: "how do I make a list NOT generic, using what some other languages call 'generics' because the *rest* of it is generic, so that a Shelf can ONLY contain Things?". Is that what you mean? If so, you can't do that quite so easily, but you can extend or wrap some container class. You'd have to tell it what class of things you want to put in, or maybe some other test. For your immediate needs, maybe something like: class Thing # insert guts of class Thing here end # simple array wrapper class Shelf def initialize @myArray = Array.new end def add thang raise 'Error: Shelves can only contain Things' if ! thang.is_a? Thing @myArray.push thang end # insert here similar calls for any other methods you want that add things # insert here any calls you want to remove things from it, look at the shelf, etc.; # some of it could probably be automagically delegated to Array with one def # of method_missing. Could probably do that with *everything* except # the calls that insert things.... end Long-term, if I needed that kind of thing a lot, I'd be tempted to make a generic wrapper and pass it a block, and say that only things that make that block return true would get inserted, with everything else causing an exception. However, that sounds to me like the kind of thing that might be needed often enough that someone might have already made a gem for it. Anybody out there know offhand? -Dave -- LOOKING FOR WORK! What: Ruby (on/off Rails), Python, other modern languages. Where: Northern Virginia, Washington DC (near Orange Line), and remote work. See: davearonson.com (main) * codosaur.us (code) * dare2xl.com (excellence). Specialization is for insects. (Heinlein) - Have Pun, Will Babble! (Aronson)