# Representing a complex object

**URL:** <https://www.ruby-forum.com/t/representing-a-complex-object/88552>\
**Category:** Ruby\
**Created:** [March 12, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552 "2007-03-12T16:44:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chandra\_shekhar\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@chandra\_shekhar\_G](https://www.ruby-forum.com/u/chandra_shekhar_G)\
**Post date:** [March 12, 2007, 4:44pm UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552/1 "2007-03-12T16:44:16Z")

</div>

Hi

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.

Regards  
chandra shekhar G

---

<div class="post-metadata">

**Author:** ![chandra\_shekhar\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@chandra\_shekhar\_G](https://www.ruby-forum.com/u/chandra_shekhar_G)\
**Post date:** [March 12, 2007, 4:50pm UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552/2 "2007-03-12T16:50:57Z")

</div>

chandra shekhar G wrote:

> Hi
> 
> 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.
> 
> Regards  
> chandra shekhar G

Do you really find this to be “complex”? It seems relatively straight  
forward…

class B  
def initialize(my\_int,my\_string)  
@my\_int = my\_int  
@my\_string = my\_string  
end  
end

class A  
def initialize  
@my\_objects = []  
end

def add\_item(item)  
@my\_objects \<\< item  
end  
end

my\_b = B.new(10,“foo”)  
my\_a = A.new  
my\_a.add\_item my\_b

---

<div class="post-metadata">

**Author:** ![chandra\_shekhar\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@chandra\_shekhar\_G](https://www.ruby-forum.com/u/chandra_shekhar_G)\
**Post date:** [March 12, 2007, 9:55pm UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552/3 "2007-03-12T21:55:47Z")

</div>

On 12.03.2007 16:44, chandra shekhar G 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.

Just a curious question: what do you need that for? What problem are  
you trying to solve?

Kind regards

robert

---

<div class="post-metadata">

**Author:** ![chandra\_shekhar\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@chandra\_shekhar\_G](https://www.ruby-forum.com/u/chandra_shekhar_G)\
**Post date:** [March 12, 2007, 5:26pm UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552/4 "2007-03-12T17:26:06Z")

</div>

On Mar 12, 9:44 am, chandra shekhar G [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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

---

<div class="post-metadata">

**Author:** ![chandra\_shekhar\_G](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/8e8cbc/32.png) [@chandra\_shekhar\_G](https://www.ruby-forum.com/u/chandra_shekhar_G)\
**Post date:** [March 16, 2007, 10:47am UTC](https://www.ruby-forum.com/t/representing-a-complex-object/88552/5 "2007-03-16T10:47:19Z")

</div>

hi

thanks for the response,  
the issue is we have an Composite object which has two types of objects  
in it  
first is a struct containing an int and a string  
second is an array of objects where each is a struct containing an int  
and a string

when we try to implement this and return it from a method,  
the api throws error saying “dont know how to cast”

any information on the same is welcome

thanks
