Gavin Kistner
3/12/2007 4:22:00 PM
On Mar 12, 9:44 am, chandra shekhar G <ychandru...@yahoo.com> wrote:
> we are facing trouble in implementing a complex object in ruby,
> like say we have a class B which has a member int and a member string,
> we have to implement a class A whose members are an array of objects of
> B
> how is it done, pls do respond.
Ruby does not care what type of value each variable is. Here's a very
simple way to do what you want:
B = Struct.new( :my_int, :my_string ) # A very simple way to create a
class
# that is just a container for
simple properties
b1 = B.new( 12, "hello" )
b2 = B.new( 42, "world" )
my_array = [ b1, b2 ] # An array of B instances
b3 = B.new( 54, "foobar" )
my_array << b3 # append one more value to the
array