# Rdoc and virtual attributes

**URL:** <https://www.ruby-forum.com/t/rdoc-and-virtual-attributes/110691>\
**Category:** Ruby\
**Created:** [July 29, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/rdoc-and-virtual-attributes/110691 "2007-07-29T19:57:16Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@stephan](https://www.ruby-forum.com/u/stephan)\
**Post date:** [July 29, 2007, 7:57pm UTC](https://www.ruby-forum.com/t/rdoc-and-virtual-attributes/110691/1 "2007-07-29T19:57:16Z")

</div>

I’ve been trying to get rdoc to document virtual attributes as though  
they weren’t virtual. The solution seems to be to use an attr\_reader /  
attr\_writer as an anchor for the documentation with the :nodoc: modifier  
on the getter / setter methods, like so:

class ClassName

# Documentation for virtual\_var.

attr\_writer :virtual\_var

def virtual\_var # :nodoc:  
# “get” virtual\_var  
end

def virtual\_var=(whatever) # :nodoc:  
# “set” virtual\_var  
end

end

Doing this results in rdoc documenting the virtual attribute alongside  
the normal attributes and not documentating the virtual attribute getter  
/ setter methods. So, to the reader, the fact that the attribute is  
virtual is completely hidden.

The main problem with this approach seems to be that it places a greater  
responsibility on the programmer to ensure that there is no mismatch  
between what a ClassName instance reports its variable to be and what  
the getter method says it is.

1. Are there any other potential problems with using this approach?

2. Is there a better way to do it?

---

<div class="post-metadata">

**Author:** ![stephan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@stephan](https://www.ruby-forum.com/u/stephan)\
**Post date:** [July 29, 2007, 8:34pm UTC](https://www.ruby-forum.com/t/rdoc-and-virtual-attributes/110691/2 "2007-07-29T20:34:00Z")

</div>

Stephan D. wrote:

> The main problem with this approach seems to be that it places a greater  
> responsibility on the programmer to ensure that there is no mismatch  
> between what a ClassName instance reports its variable to be and what  
> the getter method says it is.

Why is that a problem? The public interface should not be coupled to  
the implementation, nor expose implementation details.

Knowing that an object responds to foo= should not mean I can count on  
the object having an instance variable named @foo.

–  
James B.

“I often work by avoidance.”

- Brian Eno

---

<div class="post-metadata">

**Author:** ![stephan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8baadc/32.png) [@stephan](https://www.ruby-forum.com/u/stephan)\
**Post date:** [July 31, 2007, 1:54pm UTC](https://www.ruby-forum.com/t/rdoc-and-virtual-attributes/110691/3 "2007-07-31T13:54:05Z")

</div>

I’m just worried that by using the attr\_ methods where you’d not  
normally do so, simply for documentaion purposes, increases the risk  
that you forget their programmatic effects.

e.g. I’ve defined a virtual attribute called foo and I’ve used  
attr\_writer to document it as a normal attribute. Problem is that I’ve  
forgotten to override the setter. By inspection we can see that the use  
of foo= sets @foo, but foo doesn’t get @foo, hence showing that foo is a  
virtual attribute and exposing some of the underlying implementation.

class Bar

# foo isn’t a virtual attribute, honest!

attr\_writer :foo

# oh yes it is.

def foo # :nodoc:  
@foo+1  
end

end

irb(main):011:0\> b = Bar.new  
=\> #Bar:0xb78fd04c  
irb(main):012:0\> b.foo = 1  
=\> 1  
irb(main):013:0\> b  
=\> #\<Bar:0xb78fd04c @foo=1\>  
irb(main):014:0\> b.foo  
=\> 2

I was hoping that there was a way of flagging a method as representing a  
virtual attribute rather than having to rely on the attr\_ methods.

e.g. def foo # :virtual:
