[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: Class-level readers and writers

Christoph R.

11/30/2003 1:36:00 PM



> -----Original Message-----
> From: David A. Black [mailto:dblack@wobblini.net]
> Sent: Sunday, 30 November, 2003 03:29 AM
> To: ruby-talk ML
> Subject: Re: Class-level readers and writers
>
> Hi --
>
> On Sun, 30 Nov 2003, Gavin Sinclair wrote:
>
> > On Sunday, November 30, 2003, 8:59:15 AM, Christoph wrote:
> >
> > > David A. Black wrote:
> > > ...
> > >> So that's probably not a great idea in most cases.
> Class variables
> > >> have their own quirks, though they're scheduled to be a bit
> > >> de-quirked in 2.0.
> >
> > > My understanding (not that my record in reading Matz 2.0
> intention
> > > is anything I should be bragging about - not that I mind,
> since I am
> > > feeling like a kid in a candy store:-) was that Rite's class
> > > variables will behave like today's class instance
> variables without
> > > the need of ``class attribute accessors''.
> >
> > > Personally I am actually in favor of getting ride of
> class variables
> > > altogether and only use class attributes accessors +
> class instance
> > > variables.
> >
> > I sympathise. Maybe @@x could be syntax sugar for
> > self.class.instance_eval("@x"), if you get my drift.
> >
> > class Example
> > @x = 5
> >
> > def meth
> > @@x # -> 5
> > end
> > end
> >
> > There's probably a really good reason why not; I've never
> really been
> > able to wrap my head around all the issue here.
>
> That would create a situation where it was awfully easy for
> one object to get at another object's instance variables,
> without the second object having granted access. It's
> already possible, as your example shows, but I don't think
> it's the kind of thing one would want to encourage with
> shortcuts (unless the object itself creates them).
>
> I think the new incarnation of class variables might be
> handier than [I've found] the current one [to be], though
> sometimes I also wish they'd just disappear :-) I think part
> of the trouble people have with the concept of Class objects
> having instance variables is that they (wrongly but
> understandably) surmise that there must be some relation or
> connection with class variables.

To claim that there is no connection is a bit of stretch - as
you pointed out yourself the only difference between Rite's
class variables and (today's) instance is the additional visibility
of Rite's class variables within class instances - this additional
visibility could easily achieved with accessor methods - which
has the BIG advantage that we could do without additional
Scoping rules of yet another special kind of variables (namely
class variables) reducing the overall complexity of the language.

This simplification might be especially welcome with the introduction
of yet another kind of variables a.k.a. ``class local variables''.
I am sure you agree with me that there have been demonstrated
misunderstandings of (today's) Ruby scoping rules not only by Nubies
but by seasoned practitioners as well - lets hope that Rite's will
(okay, given Matz track record, I am certain things will be a lot
better:-) lift today's visiblity (rules) fog.

/Christoph


Just as reminder here some possible 1.8.1 implementations
of a variety of nonhierarchical class accessor methods.


---
class T
class << self
attr_accessor :var
end
def var
self.class.var
end
def var=(rhs)
self.class.var= 2
end
end

T.new.var = 2
p T.new.var # 2



class A
__var__ = nil

define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
class << self
private
def inherited(sub)
sub.instance_eval {
__var__ = nil
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
}

end
def initialize_copy(orig)
super
__var__ = nil
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
end
end
end

class B < A.clone
end
B.new.var = 2
p B.new.var


class AA
__var__ = nil

define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
class << self; self end.instance_eval {
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
}
class << self
private
def inherited(sub)
sub.instance_eval {
__var__ = nil
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
class << self; self end.instance_eval {
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
}
}

end
def initialize_copy(orig)
super
__var__ = nil
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
class << self; self end.instance_eval {
define_method(:var) {|| __var__ }
define_method(:var=) {|rhs| __var__ = rhs }
}
end
end
end

class BB < AA.clone
end


BB.new.var = 2
p BB.new.var # 2
BB.var = 3
p BB.new.var # 3
---

Ruby.