On Tue, Apr 24, 2012 at 6:24 AM, b1_ __ <lists / ruby-forum.com> wrote:
> I can't seem to find any documentation for for Ruby's attr_accessor
> method online? It is supposed to be in the Module class
> (http://ruby-doc.org/core-1.9.3/Module.html).
>
> I understand that the attr_* family of methods are private methods.
>
> I found the private methods section for the Module class and the Object
> class in the Pick Axe, but even there they are slightly hidden away
> because in the Ruby Library Reference section (pg443 of 1.9 version)
> private methods aren't mentioned in the intro bit: "Standard classes are
> listed alphabetically, followed by the standard modules. Within each, we
> list the class (or module) methods, followed by its instance methods."
> But then it goes on to list private methods in the Module and Object
> classes sections (only place private methods appear, I think?)
>
> So where can I find attr_accessor in the official Ruby 1.9.3 online
> docs?

That's pretty easy to find out:

1. Find the class via any class (which is an instance of class Class):

$ ruby19 -e 'p String.method(:attr_accessor)'
#<Method: Class(Module)#attr_accessor>
$ ruby19 -e 'p Object.method(:attr_accessor)'
#<Method: Class(Module)#attr_accessor>

We see it's defined in class Module.

2. Get the documentation:

$ ri19 -T 'Module#attr_accessor'
Module#attr_accessor

(from ruby core)
------------------------------------------------------------------------------
  attr_accessor(symbol, ...)    -> nil


------------------------------------------------------------------------------

Defines a named attribute for this module, where the name is
symbol.id2name, creating an instance variable (@name) and
a corresponding access method to read it. Also creates a method called
name= to set the attribute.

  module Mod
    attr_accessor(:one, :two)
  end
  Mod.instance_methods.sort   #=> [:one, :one=, :two, :two=]

Note: -T only omits the pager.

> And a few other related questions:
> Is this a comprehensive list of the types of methods?
> class
> instance
> private
> protected
> public (is public method the same as instance method?)

You are mixing orthogonal concepts.  There are several lists:

1. Visibility

private
protected
public

2. Receiver

instance
class (which, strictly speaking, is also only an instance; but it
often does make sense to distinguish the two in order to make it clear
what state can be accessed)

> Are there any Ruby protected methods?

Yes.

> What's the convention for writing a private method (a class method is
> written e.g. Array::new, an instance method is written e.g. Array#pop)

Do you mean in discussions?  There is no established convention for
visibility as far as I can see.  Some UML tools use prefixes

public: +
protected: o
private: -

But generally referring to the visibility explicitly gives best
results in online discussions.

Kind regards

robert


-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/