However, I'd like to be able to create a class variable that is easily
accessible (read/write) by outside users without having to write class
accessor methods. How can I do this?
A really ugly way to set the variable without an accessor would be
this:
class Foo; @@class_constant = "newvalue" end
but that is write only.
The syntax:
Foo.class_constant = "newvalue"
would be preferable, but how to do this without writing accessor
methods?
class C
class << self
attr_accessor 'class_constant'
end
end
-a
···
On Fri, 14 Jul 2006, bwv549 wrote:
It is simple to set a class constant in ruby:
class Foo
CLASS_CONSTANT = "never to change"
end
And accessed like this:
Foo::CLASS_CONSTANT
However, I'd like to be able to create a class variable that is easily
accessible (read/write) by outside users without having to write class
accessor methods. How can I do this?
A really ugly way to set the variable without an accessor would be
this:
class Foo; @@class_constant = "newvalue" end
but that is write only.
The syntax:
Foo.class_constant = "newvalue"
would be preferable, but how to do this without writing accessor
methods?
Thanks
--
suffering increases your inner strength. also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama
However, I'd like to be able to create a class variable that is easily
accessible (read/write) by outside users without having to write class
accessor methods. How can I do this?
A really ugly way to set the variable without an accessor would be
this:
class Foo; @@class_constant = "newvalue" end
but that is write only.
The syntax:
Foo.class_constant = "newvalue"
would be preferable, but how to do this without writing accessor
methods?
One way or another, if you want a class variable set when you call the
method class_constant=, then that method has to set the variable.
See Ara's answer; you're almost certainly better off using a real
attr_accessor operation on the class object. (Yes, as I pointed out,
it doesn't match the question exactly, but it's an improvement
Here's the Rails implementation if you just want to see one working
implementation. Note that it allows you to access the class attribute
from the class and from instances (which is similiar to static members
in Java, actually).
However, I'd like to be able to create a class variable that is easily
accessible (read/write) by outside users without having to write class
accessor methods. How can I do this?
A really ugly way to set the variable without an accessor would be
this:
class Foo; @@class_constant = "newvalue" end
but that is write only.
The syntax:
Foo.class_constant = "newvalue"
would be preferable, but how to do this without writing accessor
methods?
However, I'd like to be able to create a class variable that is easily
accessible (read/write) by outside users without having to write class
accessor methods. How can I do this?
A really ugly way to set the variable without an accessor would be
this:
class Foo; @@class_constant = "newvalue" end
but that is write only.
The syntax:
Foo.class_constant = "newvalue"
would be preferable, but how to do this without writing accessor
methods?
Thanks
class C
class << self
attr_accessor 'class_constant'
end
end
That doesn't use class variables, though -- which is an asset, as far
as I'm concerned but doesn't exactly match the question.
class C
class << self
attr_accessor 'class_constant'
end
end
For an intermediate ruby user, could you explain what is happening in
the line: 'class << self'
The expression:
class << obj
puts you in a class definition block for the singleton class of obj.
The singleton class of obj is where obj's singleton methods live --
that is, the methods that only obj can call.
obj = Object.new
def obj.x
"Singleton method on obj"
end
class << obj
puts instance_methods(false) # methods defined in this class only
end
Output: x
You can also define methods inside the class definition block, of
course:
class << obj
def y
"Another singleton method on obj"
end
end