Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Representing a complex object

Reply
Thread Tools

Representing a complex object

 
 
chandra shekhar G
Guest
Posts: n/a
 
      03-12-2007
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

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

 
Reply With Quote
 
 
 
 
Drew Olson
Guest
Posts: n/a
 
      03-12-2007
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

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

 
Reply With Quote
 
 
 
 
Phrogz
Guest
Posts: n/a
 
      03-12-2007
On Mar 12, 9:44 am, chandra shekhar G <(E-Mail Removed)> 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

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      03-12-2007
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
 
Reply With Quote
 
Chandra Shekar
Guest
Posts: n/a
 
      03-16-2007
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

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How complex is complex? Kottiyath Python 22 03-28-2009 10:11 PM
wsdl2java: method parameter a complex type that extends another complex type Robert Mark Bram Java 0 02-04-2007 10:06 AM
Simple pattern for representing a view of a domain object? Karl Seguin ASP .Net 1 12-09-2005 01:58 AM
[XML Schema] Content type of complex type definition with complex content Stanimir Stamenkov XML 2 10-25-2005 10:16 AM
For expert on complex loops (reposted) - complex looping problem news.amnet.net.au Java 1 04-13-2004 07:10 AM



Advertisments