Genric List in Ruby

Hi in newbie to Ruby ...But i have experience in C#...

I want to achieve Generic List in that is there in C# in Ruby.
What i want is as follows:

Class Thing
string name;
string id;
end

Class Shelf
List<Thing> lstthing = new List<Thing>;
end

Above is C# code; How can I write same in Ruby....

Please help need it urgent!!!!

Thanks in Advance

···

--
Posted via http://www.ruby-forum.com/.

Ruby is dynamically typed. Even the basic Array type can contain values
that are of any type whatsoever, so it is already a generic list which is
even more generic and flexible than the generic lists you can create in
most languages like C# and Java. It is perfectly legal to make an array
like:

[ "a", 1, :b, SomeObject.new]

where each element has a completely different type. I think it will take a
while for dynamic typing to sink in for someone like you who is used to
static types, but do take the time to understand it because it is what
makes Ruby what it is.

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

···

On Thu, Nov 24, 2011 at 04:29, Sagar Varule <sagar.varule@gmail.com> wrote:

Class Thing
string name;
string id;
end

Class Shelf
List<Thing> lstthing = new List<Thing>;
end

Above is C# code; How can I write same in Ruby....

--
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)

Also read about duck typing:

···

From my iPhone
---
Pat

On Nov 24, 2011, at 2:03 AM, Dido Sevilla <dido.sevilla@gmail.com> wrote:

Ruby is dynamically typed. Even the basic Array type can contain values
that are of any type whatsoever, so it is already a generic list which is
even more generic and flexible than the generic lists you can create in
most languages like C# and Java. It is perfectly legal to make an array
like:

[ "a", 1, :b, SomeObject.new]

where each element has a completely different type. I think it will take a
while for dynamic typing to sink in for someone like you who is used to
static types, but do take the time to understand it because it is what
makes Ruby what it is.