Class variables - a surprising result

Ged Byrne gedb01@yahoo.co.uk wrote in message news:20030822093558.59647.qmail@web10801.mail.yahoo.com

If this idiom could be replaced with some clear syntax
I think it would be a good thing.

If you want to access these in instance methods,
you need to do:

class Foo
def instance_meth
p self.class.class_eval { @var }
end
end

The beauty of ruby is that you can clean this up if you
really want.

class Module

generate “static” variables more like C++ or Java

def attr_reader_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}; @#{sym}; end] )
}
end

def attr_writer_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}=(val); @#{sym} = val; end] )
}
end

def attr_accessor_static(*syms)
attr_reader_static(syms)
attr_writer_static(syms)
end
end

class Foo
attr_accessor_static :staticvar
end

Foo.staticvar = 11
p Foo.staticvar => 11

Or even:

class Foo
class << self
attr_accessor :staticvar
end
end

Foo.staticvar = 11
p Foo.staticvar => 11

Nathaniel

<:((><

···

Alan Chen [mailto:aero6dof@yahoo.com] wrote:

The beauty of ruby is that you can clean this up if you
really want.

class Module

generate “static” variables more like C++ or Java

def attr_reader_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}; @#{sym}; end] )
}
end

def attr_writer_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}=(val); @#{sym} = val; end] )
}
end

def attr_accessor_static(*syms)
attr_reader_static(syms)
attr_writer_static(syms)
end
end

class Foo
attr_accessor_static :staticvar
end

Foo.staticvar = 11
p Foo.staticvar => 11

Hi –

Ged Byrne gedb01@yahoo.co.uk wrote in message news:20030822093558.59647.qmail@web10801.mail.yahoo.com

If this idiom could be replaced with some clear syntax
I think it would be a good thing.

If you want to access these in instance methods,
you need to do:

class Foo
def instance_meth
p self.class.class_eval { @var }
end
end

The beauty of ruby is that you can clean this up if you
really want.

class Module

generate “static” variables more like C++ or Java

def attr_reader_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}; @#{sym}; end] )
}
end

def attr_writer_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}=(val); @#{sym} = val; end] )
}
end

def attr_accessor_static(*syms)
attr_reader_static(syms)
attr_writer_static(syms)
end
end

class Foo
attr_accessor_static :staticvar
end

Foo.staticvar = 11
p Foo.staticvar => 11

That’s a bit different from just a cleaning-up of the prying into an
instance variable, though; here, the object is giving you accessor
methods, so you’re no longer prying.

I can’t help wondering why the term “static” is coming up so much. It
doesn’t seem a very good fit – nor is there a gap that needs to be
filled, as far as I can see, in the Ruby terminology. Any object can
have instance variables, and those instance variables can, if desired,
be read and/or altered through accessor methods; this is true across
the board, no more or less for Class objects than for any others. All
you have to do is go into the scope of the object’s class or singleton
class, and use the attr_* family. (Alternately, you can define the
methods directly on the object: def obj.a; @x; end and so on.)

We’ve seen several implementations where the accessor method creation
process gets wrapped in one more layer, in the single case of Class
objects, than it does for other objects. I guess some of the incentive
for this is to avoid having to jump up into the singleton class zone
(class << self…). But my impression is that the singling out of
Class objects as a kind of special case, when it comes to their having
instance variables, adds a kind of lump to the language that isn’t
really there and that doesn’t pay its way in terms of clarity.

Besides, to do the “normal” attr_* thing (i.e., with non-class objects),
you have to do:

class C
attr_accessor :a
end

which is in fact the same amount of “extra” typing as:

class << obj
attr_accessor :a
end

Hmmm… I never thought of it quite that way before :slight_smile:

David

···

On Sat, 23 Aug 2003, Alan Chen wrote:


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav

Alan Chen wrote:

The beauty of ruby is that you can clean this up if you
really want.

class Module

generate “static” variables more like C++ or Java

def attr_reader_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}; @#{sym}; end] )
}
end

def attr_writer_static(*syms)
syms.each { |sym|
module_eval( %Q[def self.#{sym}=(val); @#{sym} = val; end] )
}
end

def attr_accessor_static(*syms)
attr_reader_static(syms)
attr_writer_static(syms)
end
end

class Foo
attr_accessor_static :staticvar
end

Foo.staticvar = 11
p Foo.staticvar => 11

Actually, this should be simpler (to read, anyway):

class Object
def singleton_class
class << self
self
end
end
end

class Module
def attr_reader_static(*syms)
singleton_class.class_eval { attr_reader *syms }
end

def attr_writer_static(*syms)
   singleton_class.class_eval { attr_writer *syms }
end

def attr_accessor_static(*syms)
   singleton_class.class_eval { attr_accessor *syms }
end

end

If you want class instance variables, anyway.

If you want static variables that act like Java/C++ static variables, I
did that in a
post elsewhere in this thread (I could look it up if pressed :slight_smile: ).

  • Dan

dblack@superlink.net wrote in message news:Pine.LNX.4.44.0308221317310.4913-100000@candle

I can’t help wondering why the term “static” is coming up so much.

The “static” term comes from C++ and/or Java where it declares
a variable which is a single instance of that variable, scoped
to the class, with access properties passed by inheritance to the
children. In Ruby, the equivalent characteristics of “static”
variables are partially represented by class variables and
partially representable by instance variables on a class object.
My previous code above was mostly an excercise to see how to
reproduce C++ static variable behavior – particularly private
static variables. Of course after seeing Nathaniel’s post …
well, what’s the emoticon for slapping youself on the forehead?

Cheers,

  • alan

Hi –

···

On Sat, 23 Aug 2003, Alan Chen wrote:

dblack@superlink.net wrote in message news:Pine.LNX.4.44.0308221317310.4913-100000@candle

I can’t help wondering why the term “static” is coming up so much.

The “static” term comes from C++ and/or Java where it declares
a variable which is a single instance of that variable, scoped
to the class, with access properties passed by inheritance to the
children.

Just to clarify: I know the term, and where it comes from (at least,
the proximate source, namely C++ and Java) – it’s the relevance to
Ruby that I’m examining and wondering about, for the reasons in my
earlier post.

David


David Alan Black
home: dblack@superlink.net
work: blackdav@shu.edu
Web: http://pirate.shu.edu/~blackdav