Forum: Ruby equivalent of attr_accessor for class variables?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 bwv549 (Guest)
on 2006-07-13 22:57
(Received via mailing list)
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
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:04
(Received via mailing list)
On Fri, 14 Jul 2006, bwv549 wrote:

> accessible (read/write) by outside users without having to write class
> methods?
>
> Thanks

   class C
     class << self
       attr_accessor 'class_constant'
     end
   end

-a
883ee4d990ef9e57a79b5b0ff3b66241?d=identicon&s=25 bwv549 (Guest)
on 2006-07-13 23:16
(Received via mailing list)
>    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'

Thanks
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:17
(Received via mailing list)
Hi --

On Fri, 14 Jul 2006, ara.t.howard@noaa.gov wrote:

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


David
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:20
(Received via mailing list)
Hi --

On Fri, 14 Jul 2006, bwv549 wrote:

> accessible (read/write) by outside users without having to write class
> 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 :-)


David
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:23
(Received via mailing list)
On Fri, 14 Jul 2006, bwv549 wrote:

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

search the archives - this comes up about once per week.  for now,
suffice it
do say that

     class C
       class << self

         # everthing here is at class scope

         @a_class_instance_variable = 42

         def a_class_method() @a_class_instance_variable end

         alias_method 'one_class_method', 'to_another'

         extend ABunchOfClassMethods

       end
     end

regards.

-a
1fba4539b6cafe2e60a2916fa184fc2f?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:23
(Received via mailing list)
Hi --

On Fri, 14 Jul 2006, bwv549 wrote:

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


David
Cb48ca5059faf7409a5ab3745a964696?d=identicon&s=25 unknown (Guest)
on 2006-07-13 23:26
(Received via mailing list)
On Fri, 14 Jul 2006 dblack@wobblini.net wrote:

> That doesn't use class variables, though -- which is an asset, as far
> as I'm concerned :-)  but doesn't exactly match the question.

yeah - what he said ;-)

google the list for 'class instance variable' and 'class << self'

-a
Df5e7adb20adae6c120b04e7cafb15a0?d=identicon&s=25 Rob Sanheim (Guest)
on 2006-07-14 01:13
(Received via mailing list)
On 7/13/06, bwv549 <jtprince@gmail.com> wrote:
> accessible (read/write) by outside users without having to write class
> methods?
>
> Thanks

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).

http://dev.rubyonrails.org/svn/rails/trunk/actives...

- Rob
45196398e9685000d195ec626d477f0e?d=identicon&s=25 unknown (Guest)
on 2006-07-14 03:44
(Received via mailing list)
ara.t.howard@noaa.gov wrote:

>    class C
>      class << self
>        attr_accessor 'class_constant'
>      end
>    end

  require 'facet/functor'

  module Kernel

    # Provides access to an object's metaclass (ie. singleton)
    # by-passsing access provisions. So for example:
    #
    #   class X
    #     meta.attr_accesser :a
    #   end
    #
    #   X.a = 1
    #   X.a #=> 1

    def meta
      @_meta_functor ||= Functor.new do |op,*args|
        (class << self; self; end).send(op,*args)
      end
    end

  end

T.
This topic is locked and can not be replied to.