attr_{reader|writer|accessor} for class methods 
Author Message
 attr_{reader|writer|accessor} for class methods

Hi,
how do I implement an atribute reader/writer/accessor for a class variable?

Something on the lines of

class Foo
        attr_reader :Foo.bar    

end

puts Foo.bar
Foo.bar="wibble"
puts Foo.bar

although this gives the following:
/home/stehill1/tmp/attr.rb:4: undefined method `bar' for :Foo:Symbol (NameError)

Thanks

Steve



Sun, 04 Jul 2004 22:39:20 GMT  
 attr_{reader|writer|accessor} for class methods

Quote:

> class Foo
>    attr_reader :Foo.bar    

> end

The reason this doesn't work is because :Foo.bar is the same as
(:Foo).bar.  If you want to do something like this, try:

  http://rm-f.net/~cout/ruby/treasures/RubyTreasures-0.1/lib/hacks/acce...

Paul



Sun, 04 Jul 2004 23:10:45 GMT  
 attr_{reader|writer|accessor} for class methods

Quote:

> how do I implement an atribute reader/writer/accessor for a class variable?

You have to define the attributes in the context of the class's class:

  class Dave
    class <<self
      attr_accessor :wombat
    end
    # etc...
  end

  Dave.wombat = 123

Dave



Sun, 04 Jul 2004 23:32:22 GMT  
 attr_{reader|writer|accessor} for class methods

...

http://rm-f.net/~cout/ruby/treasures/RubyTreasures-0.1/lib/hacks/acce...
ml
..
Hm there are a couple of typos in your solution (and Dave's wombat is
a completely different animal - a.k.a. a ``per class instance'' attribute) -

------
--- accessors.rb.new    Wed Jan 16 18:11:59 2002

       self.class_eval %{
         ##


         def self.#{symbol}


         end
       %}

         ##

         def self.#{symbol}(x)


         end
       %}
     end
------

/Christoph



Mon, 05 Jul 2004 10:23:24 GMT  
 attr_{reader|writer|accessor} for class methods

Quote:

> Hm there are a couple of typos in your solution (and Dave's wombat is
> a completely different animal - a.k.a. a ``per class instance'' attribute) -

I was about to reply and say that it looked fine to me, but then I
realized that the diff was backward (it's diff from-file to-file, not
the other way around).

Thanks for pointing out my error; I've fixed this in CVS.

Paul



Mon, 05 Jul 2004 03:30:49 GMT  
 attr_{reader|writer|accessor} for class methods

...

Quote:
> I was about to reply and say that it looked fine to me, but then I
> realized that the diff was backward (it's diff from-file to-file, not
> the other way around).

Sorry I always have problems with things like left/right, +/- etc.
and I forgot to test my patch as well;-)  - i.e. I forgot to remove the
unnecessary  ``%''  infront of the closing  ``}'' for the class_eval
argument.

/Christoph



Mon, 05 Jul 2004 13:04:25 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. addign class attr reader in C extension

2. function as attr of subclass != method (???)

3. Accessor methods

4. Accessor Methods - Why?

5. Defining accessor methods

6. proposal: accessor methods

7. need help: funcall and accessor methods

8. How can I redefine accessors (and method) in Full CLOS (compared to AMOP)

9. setf methods for structure accessors used as parameters

10. Accessor methods question

11. Graphic-file readers and writers

12. Writers wanted, readers needed

 

 
Powered by phpBB® Forum Software