Ruby. A problem with composition.

For general discussion related FlowStone
Post Reply
User avatar
Parki
Posts: 20
Joined: Fri Feb 27, 2015 11:12 am

Ruby. A problem with composition.

Post 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
I hope my english was understeandable.
Skype id - parkidesu
User avatar
MyCo
Posts: 718
Joined: Tue Jul 13, 2010 12:33 pm
Location: Germany
Contact:

Re: Ruby. A problem with composition.

Post 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
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Ruby. A problem with composition.

Post 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.
"There lies the dog buried" (German saying translated literally)
Post Reply