From: Michael Fellinger Date: 2010-06-20T01:20:26+09:00 Subject: Re: General Ruby OOP question On Sun, Jun 20, 2010 at 12:37 AM, Ammar Ali wrote: > On Sat, Jun 19, 2010 at 4:56 PM, Paul A. wrote: >> But is it ethics? I mean, is that lawful under the principles of >> object-oriented programming. Maybe this is so much power, to pass self >> inside person instance inside its home... 'cause it's almost as if there >> was no class, no partitioning. > > There is nothing wrong from an OOP point of view with one object > containing a reference to another object. Even in the real world, it > makes sense that a person knows where their home is. > > In some languages, like C++, this can introduce some typing and API > complications, but not in Ruby where methods are invoked by sending > messages to the objects. > > The problems that you might have with this approach are related to > design requirements. For example, what if one person can, optionally, > have more than one home? I think I'd model it somewhat like this (untested): class Window attr_accessor :open end class Home attr_accessor :window def initialize @window = Window.new end end class Person attr_accessor :location def enter(location) self.location = location end def open_window if location.window.respond_to?(:open=) location.window.open = true else raise("The location doesn't have any windows you can open") end end end guy = Person.new home = Home.new guy.enter(home) guy.open_window -- Michael Fellinger CTO, The Rubyists, LLC