Page 1 of 1

Ruby. A problem with composition.

Posted: Sat Jun 27, 2015 8:59 pm
by Parki
Is it possible to make this without going A class outside?
Because when I trying to run the second one it's won't work:

Code: Select all

class A
   def initialize
      @a = 0
   end
   def event_stuff
      @a += 1
   end
   def get_a
      @a
   end
end

class RubyEdit
   def init
      @a = A.new
   end
   def event
      @a.event_stuff
      watch @a.get_a
   end
end

#########################

class A
   def initialize
      @a = 0
   end
   def event_stuff
      @a += 1
   end
   def get_a
      @a
   end
end
class B
   def initialize
      @a = A.new
   end
   def get_a
      @a
   end
   def watch_a
      @a.get_a
   end
end

class RubyEdit
   def init
      @a = B.new
   end
   def event
      @a.get_a.get_a
      watch @a.watch_a
   end
end

Re: Ruby. A problem with composition.

Posted: Sat Jun 27, 2015 10:46 pm
by MyCo
not sure if I understand correctly, but I think the problem in the second code is here:

Code: Select all

class RubyEdit
   def init
      @a = B.new
   end
   def event
      @a.get_a.get_a  # <<--- "get_a", not "event_stuff" ?
      watch @a.watch_a
   end
end

Re: Ruby. A problem with composition.

Posted: Sun Jun 28, 2015 5:50 am
by tulamide
Like Maik said, if you are wondering why the second one doesn't count up, it's because "event_stuff" isn't called. Otherwise, you should provide an example of what you're trying to achieve.