Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Class instance variable idiom

Reply
Thread Tools

Class instance variable idiom

 
 
Perry Smith
Guest
Posts: n/a
 
      10-17-2007
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-forum.com/.

 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      10-17-2007
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
 
Reply With Quote
 
 
 
 
David A. Black
Guest
Posts: n/a
 
      10-17-2007
Hi --

On Thu, 18 Oct 2007, 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?


Yes:

class C
class << self
attr_accessor :foo
end
end


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
Reply With Quote
 
Perry Smith
Guest
Posts: n/a
 
      10-17-2007
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-forum.com/.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      10-17-2007
Hi --

On Thu, 18 Oct 2007, Perry Smith wrote:

> 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?


Yes. The call to attr_accessor will create singleton methods for use
by the class object Foo.


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
Reply With Quote
 
Gary Wright
Guest
Posts: n/a
 
      10-17-2007

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

 
Reply With Quote
 
Brian Adkins
Guest
Posts: n/a
 
      10-18-2007
On Oct 17, 4:10 pm, Perry Smith <(E-Mail Removed)> 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?

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      10-18-2007
2007/10/18, Brian Adkins <(E-Mail Removed)>:
> On Oct 17, 4:10 pm, Perry Smith <(E-Mail Removed)> 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

 
Reply With Quote
 
Brian Adkins
Guest
Posts: n/a
 
      10-18-2007
On Oct 18, 2:44 am, "Robert Klemme" <(E-Mail Removed)>
wrote:
> 2007/10/18, Brian Adkins <(E-Mail Removed)>:
> > On Oct 17, 4:10 pm, Perry Smith <(E-Mail Removed)> 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.

 
Reply With Quote
 
David A. Black
Guest
Posts: n/a
 
      10-18-2007
Hi --

On Fri, 19 Oct 2007, Brian Adkins wrote:

> On Oct 18, 2:44 am, "Robert Klemme" <(E-Mail Removed)>
> wrote:
>> 2007/10/18, Brian Adkins <(E-Mail Removed)>:
>>> On Oct 17, 4:10 pm, Perry Smith <(E-Mail Removed)> 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.


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.

The prefix @@ makes it appear that they have some connection or
kinship with instance variables. Actually, they're almost the
opposite. Instance variables represent state on a strictly per-object
basis. Class variables cut across many different objects and scopes.
I would prefer them to look like $$this, since they're really a
kind of restricted global variable rather than an extended instance
variable.

The worst thing about them, in my view, is the amount of confusion
they have caused. The fact that there is something called a "class
variable" -- and that it looks vaguely like an instance variable --
has been a huge obstacle for people trying to get a grasp on the idea
that classes are objects and can, via *instance* variable, have their
own state. Class variables throw a big shadow over that otherwise very
consistent and lucid state of things. I've seen this happen again and
again for seven years.

In Ruby > 1.8, class variables are going to be somewhat more strictly
per-class. That's a mixed blessing. They're going to come closer to
representing a class's state, but then the question will arise: why
have both that *and* instance variables of classes? It's not
impossible to answer that, but it's not a bad question.

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.


David

--
Upcoming training from Ruby Power and Light, LLC:
* Intro to Ruby on Rails, Edison, NJ, October 23-26
* Advancing with Rails, Edison, NJ, November 6-9
Both taught by David A. Black.
See http://www.rubypal.com for more info!

 
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
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Problem when subclass instance changes base class instance variable Gerry Sutton Python 1 04-16-2005 06:06 AM
accessing class instance variable from instance method David Garamond Ruby 5 06-08-2004 02:26 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments