General Ruby OOP question

Discussion in 'Ruby' started by Paul A., Jun 19, 2010.

  1. Paul A.

    Paul A. Guest

    Hello,

    I have a general question about OOP with Ruby. If I have 2 class: Home
    and Person for instance, such as:

    >> class Home
    >> def initialize
    >> @person1 = Person.new
    >> @person2 = Person.new
    >> # ...
    >> end
    >>
    >> def open_a_window; end
    >> # ...
    >> end


    >> class Person
    >> def play_wii_game; end
    >> end


    Then, a home instance can include a lot of people. And that's cool. And
    a home instance can apply action on a person using a method just like:
    @person2.play_wii_game

    But if a person want to open a window in its home? Here, with a such
    design it's impossible, isn't it.

    So to allow this kind of action, I think we need to pass as parameter
    self inside Home initialization process, becoming:

    >> class Home
    >> def initialize
    >> @person1 = Person.new(self)
    >> @person2 = Person.new(self)
    >> # ...
    >> end
    >>
    >> def open_a_window; end
    >> # ...
    >> end


    >> class Person
    >> def initialize(home)
    >> @home = home
    >> end
    >>
    >> def open_a_window
    >> @home.open_a_window
    >> end
    >>
    >> def play_wii_game; end
    >> end


    Now, a instantiated person can do it using open_a_window proxy method.
    And any other proxifyable home's methods.

    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.

    What do you think about this?
    Thanks for any considerations.
    --
    Posted via http://www.ruby-forum.com/.
    Paul A., Jun 19, 2010
    #1
    1. Advertising

  2. Paul A.

    Intransition Guest

    On Jun 19, 9:56=A0am, "Paul A." <> wrote:
    > Hello,
    >
    > I have a general question about OOP with Ruby. =A0If I have 2 class: Home
    > and Person for instance, such as:
    >
    > >> class Home
    > >> =A0 def initialize
    > >> =A0 =A0 @person1 =3D Person.new
    > >> =A0 =A0 @person2 =3D Person.new
    > >> =A0 =A0 # ...
    > >> =A0 end

    >
    > >> =A0 def open_a_window; end
    > >> =A0 # ...
    > >> end
    > >> class Person
    > >> =A0 def play_wii_game; end
    > >> end

    >
    > Then, a home instance can include a lot of people. And that's cool. =A0An=

    d
    > a home instance can apply action on a person using a method just like:
    > @person2.play_wii_game
    >
    > But if a person want to open a window in its home? =A0Here, with a such
    > design it's impossible, isn't it.
    >
    > So to allow this kind of action, I think we need to pass as parameter
    > self inside Home initialization process, becoming:
    >
    >
    >
    > >> class Home
    > >> =A0 def initialize
    > >> =A0 =A0 @person1 =3D Person.new(self)
    > >> =A0 =A0 @person2 =3D Person.new(self)
    > >> =A0 =A0 # ...
    > >> =A0 end

    >
    > >> =A0 def open_a_window; end
    > >> =A0 # ...
    > >> end
    > >> class Person
    > >> =A0 def initialize(home)
    > >> =A0 =A0 @home =3D home
    > >> =A0 end

    >
    > >> =A0 def open_a_window
    > >> =A0 =A0 @home.open_a_window
    > >> =A0 end

    >
    > >> =A0 def play_wii_game; end
    > >> end

    >
    > Now, a instantiated person can do it using open_a_window proxy method.
    > And any other proxifyable home's methods.
    >
    > 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.
    >
    > What do you think about this?
    > Thanks for any considerations.


    It's fine. But why is Home creating people? Perhaps they should be
    added to a home? In which case you could create a home and then a
    person with a home and the person would be automatically added to that
    home.

    home =3D Home.new
    person1 =3D Person.new(home1)

    Or you could create both the home and person separately but when you
    add the person to the home they will pick up a reference to it.

    home =3D Home.new
    person1 =3D Person.new
    home << person1

    In Home:

    class Home

    def <<(person)
    @persons << person
    person.home =3D self
    end

    ~trans
    Intransition, Jun 19, 2010
    #2
    1. Advertising

  3. Paul A.

    Ammar Ali Guest

    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?


    Ammar
    Ammar Ali, Jun 19, 2010
    #3
  4. 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 :eek:pen
    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
    Michael Fellinger, Jun 19, 2010
    #4
  5. Paul A.

    Josh Cheek Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Sat, Jun 19, 2010 at 8:56 AM, Paul A. <> wrote:

    > Hello,
    >
    > I have a general question about OOP with Ruby. If I have 2 class: Home
    > and Person for instance, such as:
    >
    > >> class Home
    > >> def initialize
    > >> @person1 = Person.new
    > >> @person2 = Person.new
    > >> # ...
    > >> end
    > >>
    > >> def open_a_window; end
    > >> # ...
    > >> end

    >
    > >> class Person
    > >> def play_wii_game; end
    > >> end

    >
    > Then, a home instance can include a lot of people. And that's cool. And
    > a home instance can apply action on a person using a method just like:
    > @person2.play_wii_game
    >
    > But if a person want to open a window in its home? Here, with a such
    > design it's impossible, isn't it.
    >
    > So to allow this kind of action, I think we need to pass as parameter
    > self inside Home initialization process, becoming:
    >
    > >> class Home
    > >> def initialize
    > >> @person1 = Person.new(self)
    > >> @person2 = Person.new(self)
    > >> # ...
    > >> end
    > >>
    > >> def open_a_window; end
    > >> # ...
    > >> end

    >
    > >> class Person
    > >> def initialize(home)
    > >> @home = home
    > >> end
    > >>
    > >> def open_a_window
    > >> @home.open_a_window
    > >> end
    > >>
    > >> def play_wii_game; end
    > >> end

    >
    > Now, a instantiated person can do it using open_a_window proxy method.
    > And any other proxifyable home's methods.
    >
    > 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.
    >
    > What do you think about this?
    > Thanks for any considerations.
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    Plus, you get a nice little benefit in that the Person#open_a_window method
    can contain the conditionals for dealing with the edge case of when there is
    no home.

    class Person
    def open_a_window
    @home.open_a_window if @home
    end
    def has_home?
    !!@home
    end
    end

    Then later you can say things like
    person.open_a_window if person.hot?

    rather than having to cart the exception logic all over your implementaion
    person.open_a_window if person.hot? && person.has_home?

    Though, I suppose, it is a rather limited model in that your person may be
    at work, or at a friend's home.
    Josh Cheek, Jun 19, 2010
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. antoine
    Replies:
    7
    Views:
    466
    Rhino
    Mar 3, 2005
  2. Greg Brondo

    Ruby general question

    Greg Brondo, Jan 3, 2004, in forum: Ruby
    Replies:
    7
    Views:
    152
  3. David Espada
    Replies:
    2
    Views:
    136
    David Espada
    Jul 1, 2004
  4. Replies:
    9
    Views:
    163
    John Wilger
    Mar 16, 2007
  5. Lucas Holland

    Defending Ruby's OOP

    Lucas Holland, May 10, 2007, in forum: Ruby
    Replies:
    31
    Views:
    331
    Robert Dober
    May 16, 2007
Loading...

Share This Page