[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

instance variables available in derived clases

Richard Roberts

1/27/2007 11:29:00 AM

Hi.

Please read my <a
href="http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quo...
post</a>, regarding instance variables in derived classes.

Does anyone know a way to make instance variables 'private'?

Cheers,
Rich.

--
Posted via http://www.ruby-....

17 Answers

Richard Roberts

1/27/2007 11:31:00 AM

0

Ah - I see putting html into the posts doesn't work! The blog url is

http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-de...

:)

--
Posted via http://www.ruby-....

WoNáDo

1/27/2007 11:50:00 AM

0

Richard Roberts schrieb:
> Hi.
>
> Please read my <a
> href="http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quo...
> post</a>, regarding instance variables in derived classes.
>
> Does anyone know a way to make instance variables 'private'?
>
> Cheers,
> Rich.
>

Instance variables are part of an object, they are the attributes that contain
values special for each instance.

Fro an OO point of view a derived class can only extend an excisting class. This
means all objects of the parent class are still relevant for the child, plus
additional ones.

So it doesn't make any sense to hide these instance variables.

For local working inside a method "local variables" are available.

btw - There is one method in Ruby which I sometimes use special cases, and which
breakes the rule, that a derived class should only extend a class, an not
shorten it's capabilities: "Module#undef_method".

Wolfgang Nádasi-Donner

WoNáDo

1/27/2007 11:52:00 AM

0

Richard Roberts schrieb:
> Hi.
>
> Please read my <a
> href="http://richtextblog.blogspot.com/2007/01/ruby-instance-variables-in-derived.html&quo...
> post</a>, regarding instance variables in derived classes.
>
> Does anyone know a way to make instance variables 'private'?
>
> Cheers,
> Rich.
>

Instance variables are part of an object, they are the attributes that contain
values special for each instance.

From an OO point of view a derived class can only extend an excisting class.
This means all objects of the parent class are still relevant for the child,
plus additional ones.

So it doesn't make any sense to hide these instance variables.

For local working inside a method "local variables" are available.

btw - There is one method in Ruby which I sometimes use for special cases, and
which breakes the rule, that a derived class should only extend a class, an not
shorten it's capabilities: "Module#undef_method".

Wolfgang Nádasi-Donner

Richard Roberts

1/27/2007 12:08:00 PM

0

Hi Wolfgang. Thanks for your response.

I agree in many cases derived classes just extend an existing class, and
all objects of the parent class are relevent to the children. In fact,
I have often written quite a base class quite defensively in C#, only to
find that I do indeed need to expose further properties to the derived
ones later.

However, sometimes it is good to keep stuff in the base class private.
What if there was an algorithm in the base class which depended on a
class level instance variable to govern its behaviour? In ruby, all
derived classes would be allowed to change this variable, possibly
breaking or affecting it's operation (especially if the derived class
was written by a different developer).

In rails, you can set instance variables in the ApplicationController,
and they're automatically available in other controllers. The behaviour
of ruby I described means that in the other controllers you've got to be
careful not to accidentally overwrite this value.

i.e. We have not explicitly exposed the instance variable in the base
class but in the derived classes we still need to know about the
implementation of the base class. This kind of breaks the orthogonality
of the design, doesn't it?

Rich.

Wolfgang Nádasi-donner wrote:
> Richard Roberts schrieb:
>>
> Instance variables are part of an object, they are the attributes that
> contain
> values special for each instance.
>
> From an OO point of view a derived class can only extend an excisting
> class.
> This means all objects of the parent class are still relevant for the
> child,
> plus additional ones.
>
> So it doesn't make any sense to hide these instance variables.
>
> For local working inside a method "local variables" are available.
>
> btw - There is one method in Ruby which I sometimes use for special
> cases, and
> which breakes the rule, that a derived class should only extend a class,
> an not
> shorten it's capabilities: "Module#undef_method".
>
> Wolfgang Nádasi-Donner


--
Posted via http://www.ruby-....

Richard Roberts

1/27/2007 4:06:00 PM

0

Anyone else got views on this??

--
Posted via http://www.ruby-....

Gavin Kistner

1/27/2007 4:15:00 PM

0

On Jan 27, 9:05 am, Richard Roberts <ricisb...@yahoo.co.uk> wrote:
> Anyone else got views on this??

It looks like Ruby 1.9 will give you the ability to define private
methods in the base class (which may be simple getter/setter methods)
that are accessible to the base class, but not the descendents. Of
course the subclass will still have access to the instance variables -
in my opinion it would be madness to have 'inheritance' without this.

Avdi Grimm

1/27/2007 7:44:00 PM

0

To the contrary, it's not madness at all. It's a legitimate
interpretation of OO principles to say that a private member variable
is private to a class, and should be inaccessible to all clients of
the class - INCLUDING derived classes. Because your class may be used
as a base class by other programmers in other projects, it's important
to be able to define a stable interface not only to the class's
collaborators (other classes), but to it's children as well. That way
other programmers can build on your class without worrying about the
changes in implementation.

This is precisely why both C++ and Java make a distinction between
"private" and "protected". The former is for internal use only by the
class; the latter is the interface the class exposes to it's
descendants. And public, of course, is the interface it exposes to
the outside world.

Actually, I wouldn't mind seeing this in Ruby. Ruby's approach is
very flexible and convenient, but it carries with it the danger that
if you derive from a third-party class (like, say, ActiveRecord), you
might inadvertently overwrite a member variable that the base class
uses.

--
Avdi

On 1/27/07, Phrogz <gavin@refinery.com> wrote:
> On Jan 27, 9:05 am, Richard Roberts <ricisb...@yahoo.co.uk> wrote:
> > Anyone else got views on this??
>
> It looks like Ruby 1.9 will give you the ability to define private
> methods in the base class (which may be simple getter/setter methods)
> that are accessible to the base class, but not the descendents. Of
> course the subclass will still have access to the instance variables -
> in my opinion it would be madness to have 'inheritance' without this.
>
>
>

Richard Roberts

1/27/2007 7:51:00 PM

0

I'm glad not everyone thinks I'm mad!

Does anyone know a good way to simulate the C++ and Java behaviour in
ruby? I've tried various things, but to no avail (e.g. declaring them as
class-level instance variables in a private block, creating private
accessors etc).

Avdi Grimm wrote:
"> To the contrary, it's not madness at all"


--
Posted via http://www.ruby-....

Vincent Fourmond

1/27/2007 8:11:00 PM

0

Richard Roberts wrote:
> I'm glad not everyone thinks I'm mad!
>
> Does anyone know a good way to simulate the C++ and Java behaviour in
> ruby? I've tried various things, but to no avail (e.g. declaring them as
> class-level instance variables in a private block, creating private
> accessors etc).

Write a C structure holding your variables (it doesn't even have to be
complex: a VALUE array should do). Apart from that, no solution. You
won't change easily a design feature of Ruby ;-)...

Vince

--
Vincent Fourmond, PhD student (not for long anymore)
http://vincent.fourmon...

WoNáDo

1/27/2007 8:38:00 PM

0

Avdi Grimm schrieb:
> To the contrary, it's not madness at all. It's a legitimate
> interpretation of OO principles to say that a private member variable
> is private to a class, and should be inaccessible to all clients of
> the class - INCLUDING derived classes.

I think this not possible in Ruby in general, because onybody can change a class
later.

It's normal practice to write code in Ruby, which changes classes of the
standard library.

>>>>> Example >>>>>

class Otto
def show
puts "here is 'show'"
end
private
def hiddenshow
puts "here is 'hiddenshow'"
end
end

o = Otto.new
o.show
o.hiddenshow rescue puts '+++ cannot call hiddenshow'

class Otto
public :hiddenshow
end

o.hiddenshow

>>>>> Output >>>>>

here is 'show'
+++ cannot call hiddenshow
here is 'hiddenshow'

>>>>> EoE >>>>>

Wolfgang Nádasi-Donner