Quoteing (e-mail address removed), on Mon, Nov 08, 2004 at 08:37:33AM +0900:
Sam said:
Quoteing (e-mail address removed), on Mon, Nov 08, 2004 at 04:26:26AM
+0900:
class Foo
attr_reader :bar
end
f = Foo.new
methods = f.methods - Object.methods
p methods # ["bar"]
Ruby seems to think I've defined a method.
If it's a method, why not document it as a method?
By this definition, there is no such thing as an attribute, only
methods.
That is my understanding of Ruby. I may be wrong.
Not sure. If it's your understanding that all attributes are methods,
then you are right.
If you are of the understanding that there are no attributes, you are
wrong, an attribute is a method defined with attr_*, etc., as you
demonstrate above.
(I take the view that 'attributes' are a private aspect of an object,
and that all object access (barring reflection, et al) occurs via
messages. I'm one of those who see "@foo" and read it as, "the
attribute foo." "attr_reader :foo " creates a method to access this
attribute, @foo. I know from prior discussions that this is likely a
minority view. )
It's a reasonable view. There's lots of differing definitions of object,
attribute, function, etc., and that can be yours. But, if you don't use
the definitions customary with the language you're working with, nobody
will know what you're talking about!
Right. I see this as a developer convenience for creating methods. An
implementation detail, not an expression of intent that should be
exposed in the API documentation.
Ah, and there we differ. It's quite useful, IMO, to think of objects in
terms of set/get APIs (aka "attributes"), and APIs that do things. That
Ruby implements all of these as methods is cool, but its still useful to
explain some of my classes to PEOPLE in terms of the distinction, even
if the ruby interpreter doesn't see a distinction.
As it is, developers who want to use the short form to define attribute
accessor methods have to accept that the documentation will refer to
these methods as something other than methods.
There's a good chance you can play some games with alias to make new
names for attr_* and friends, and that that would fool rdoc, and still
give you convenience.
For that matter, you could just write your own code that takes a
variable list of symbols and defines a bunch of methods. I'm not good
enough with this stuff to type out a solution off the top of my head,
but I don't think there's anything attr_reader does that you can't
implement yourself, getting both the convenience, and getting them
doc'ed as you like, and not confusing readers of your code who might
share the more common community view of what an attribute is.
Have fun,
Sam