[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Class instance variable idiom

Perry Smith

10/17/2007 8:11:00 PM

I am using this technique for class instance variables:

class << self
def set_foo(s)
@foo = s
end
alias :foo= :set_foo

def foo
@foo
end
end

Then to reference foo from an instance of the class (or subclass), I do:

self.class.foo

Is there a cleaner way, in particular for the reference of the class
instance variable, to do this?

Thank you,
pedz
--
Posted via http://www.ruby-....

26 Answers

Robert Klemme

10/17/2007 8:12:00 PM

0

On 17.10.2007 22:10, Perry Smith wrote:
> I am using this technique for class instance variables:
>
> class << self
> def set_foo(s)
> @foo = s
> end
> alias :foo= :set_foo
>
> def foo
> @foo
> end
> end
>
> Then to reference foo from an instance of the class (or subclass), I do:
>
> self.class.foo
>
> Is there a cleaner way, in particular for the reference of the class
> instance variable, to do this?

class << self
attr_accessor :foo
end

robert

David A. Black

10/17/2007 8:14:00 PM

0

Perry Smith

10/17/2007 8:22:00 PM

0

Robert Klemme wrote:
> On 17.10.2007 22:10, Perry Smith wrote:
>> end
>> end
>>
>> Then to reference foo from an instance of the class (or subclass), I do:
>>
>> self.class.foo
>>
>> Is there a cleaner way, in particular for the reference of the class
>> instance variable, to do this?
>
> class << self
> attr_accessor :foo
> end
>
> robert

Thanks!

Do I still do the self.class.foo to access it?

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

David A. Black

10/17/2007 8:26:00 PM

0

Gary Wright

10/17/2007 8:27:00 PM

0


On Oct 17, 2007, at 4:22 PM, Perry Smith wrote:

> Do I still do the self.class.foo to access it?

yes, and

self.class.foo = obj

to invoke the writer/setter method.

Gary Wright

Brian Adkins

10/18/2007 5:10:00 AM

0

On Oct 17, 4:10 pm, Perry Smith <p...@easesoftware.com> wrote:
> I am using this technique for class instance variables:
>
> class << self
> def set_foo(s)
> @foo = s
> end
> alias :foo= :set_foo
>
> def foo
> @foo
> end
> end
>
> Then to reference foo from an instance of the class (or subclass), I do:
>
> self.class.foo
>
> Is there a cleaner way, in particular for the reference of the class
> instance variable, to do this?

Just out of curiosity, why do you want a "class instance variable"
instead of a class variable?

Robert Klemme

10/18/2007 6:44:00 AM

0

2007/10/18, Brian Adkins <lojicdotcom@gmail.com>:
> On Oct 17, 4:10 pm, Perry Smith <p...@easesoftware.com> wrote:
> > I am using this technique for class instance variables:
> >
> > class << self
> > def set_foo(s)
> > @foo = s
> > end
> > alias :foo= :set_foo
> >
> > def foo
> > @foo
> > end
> > end
> >
> > Then to reference foo from an instance of the class (or subclass), I do:
> >
> > self.class.foo
> >
> > Is there a cleaner way, in particular for the reference of the class
> > instance variable, to do this?
>
> Just out of curiosity, why do you want a "class instance variable"
> instead of a class variable?

Probably because class variables have @@various issues as has
discusses before. I would also recommend to not use them.

Kind regards

robert

Brian Adkins

10/18/2007 4:24:00 PM

0

On Oct 18, 2:44 am, "Robert Klemme" <shortcut...@googlemail.com>
wrote:
> 2007/10/18, Brian Adkins <lojicdot...@gmail.com>:
> > On Oct 17, 4:10 pm, Perry Smith <p...@easesoftware.com> wrote:
> > > I am using this technique for class instance variables:
> > Just out of curiosity, why do you want a "class instance variable"
> > instead of a class variable?
>
> Probably because class variables have @@various issues as has
> discusses before. I would also recommend to not use them.

What issues have you experienced with them to cause you to not
recommend them? I rarely have a need for either class variables or
"class instance variables", but I've yet to experience any problems
with class variables.

I think they have different purposes, strengths and weaknesses, but I
wouldn't recommend not using class variables, so I'm curious if I'm
missing some problem with them.

David A. Black

10/18/2007 5:23:00 PM

0

Rick DeNatale

10/18/2007 7:39:00 PM

0

On 10/18/07, David A. Black <dblack@rubypal.com> wrote:

> I'm on record as saying that class variables are the thing I dislike
> most about Ruby, so I'll try to explain why.
>
> They break encapsulation. They are visible to a weird assortment of
> objects: class C, instances of C, class D < C, instances of D... all
> the same variable. This cross-section of objects doesn't have anything
> in common except that they can all see each other's class variables.
And even that is squirrely. Visibility depends on sequence of
appearance, watch carefully, my fingers will ever leave my hands! <G>
>
class A
@@x = "A"

def self.show_x
@@x
end
end
A.show_x # => "A"

class B < A
end
B.show_x # => "A"

class B
@@x = "B" # !> already initialized class variable @@x
end
B.show_x # => "B"
A.show_x # => "B"

class C
end

class D < C
@@x = "D"
def self.show_x
@@x # !> class variable @@x of C is overridden by D
end
end
D.show_x # => "D"

class C
@@x = "C"
def self.show_x
@@x
end
end
D.show_x # => "D"
C.show_x # => "C"

class D
@@x = "Blorp" # !> class variable @@x of C is overridden by D
end
D.show_x # => "Blorp"
C.show_x # => "C"

> That sums up my views. Just so you know: I love Ruby madly :-) This is
> one of the very few areas where I dissent strongly from the way it's
> designed.

Amen, brother!

--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...