Forum: Ruby Representing a complex object

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
03b557a840fc10a1af8fad2c4d05e837?d=identicon&s=25 chandra shekhar G (Guest)
on 2007-03-12 16:44
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
94cc3e46cfc5bc361e409e2e884ecfa4?d=identicon&s=25 Drew Olson (dfg59)
on 2007-03-12 16:50
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
852a62a28f1de229dc861ce903b07a60?d=identicon&s=25 Gavin Kistner (phrogz)
on 2007-03-12 17:26
(Received via mailing list)
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
E0d864d9677f3c1482a20152b7cac0e2?d=identicon&s=25 Robert Klemme (Guest)
on 2007-03-12 21:55
(Received via mailing list)
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
03b557a840fc10a1af8fad2c4d05e837?d=identicon&s=25 Chandra Shekar (chandraruby)
on 2007-03-16 10:47
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
This topic is locked and can not be replied to.