Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > General Ruby OOP question

Reply
Thread Tools

General Ruby OOP question

 
 
Paul A.
Guest
Posts: n/a
 
      06-19-2010
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/.

 
Reply With Quote
 
 
 
 
Intransition
Guest
Posts: n/a
 
      06-19-2010


On Jun 19, 9:56=A0am, "Paul A." <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Ammar Ali
Guest
Posts: n/a
 
      06-19-2010
On Sat, Jun 19, 2010 at 4:56 PM, Paul A. <(E-Mail Removed)> 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

 
Reply With Quote
 
Michael Fellinger
Guest
Posts: n/a
 
      06-19-2010
On Sun, Jun 20, 2010 at 12:37 AM, Ammar Ali <(E-Mail Removed)> wrote:
> On Sat, Jun 19, 2010 at 4:56 PM, Paul A. <(E-Mail Removed)> 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 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?(pen=)
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

 
Reply With Quote
 
Josh Cheek
Guest
Posts: n/a
 
      06-19-2010
[Note: parts of this message were removed to make it a legal post.]

On Sat, Jun 19, 2010 at 8:56 AM, Paul A. <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
General....very general.... no important for u forever hi Python 0 03-18-2009 08:21 AM
Defending Ruby's OOP Lucas Holland Ruby 31 05-16-2007 09:19 AM
General Ruby OOP Question - using inheritance or include for shared attributes james.d.masters@gmail.com Ruby 9 03-16-2007 01:59 AM
General Question about java parameters / OOP good design techniques antoine Java 7 03-03-2005 05:01 AM
[ruby-gnome2] general memory problems with ruby-gnome2? David Espada Ruby 2 07-01-2004 02:18 PM



Advertisments