Where is attr_accessor located?

Discussion in 'Ruby' started by Mariano Kamp, Nov 12, 2006.

  1. Mariano Kamp

    Mariano Kamp Guest

    Hi,

    I am a bit confused. I get different responses when asking RI and
    IRB. It's probably just me not understanding to read the output or
    issue the right queries.

    When calling "ri attr_accesor" I get the following:

    localhost:~ mkamp$ ri attr_accessor
    --------------------------------------------------- Module#attr_accessor
    attr_accessor(symbol, ...) => nil
    ------------------------------------------------------------------------
    Equivalent to calling ``+attr+_symbol_+, true+'' on each _symbol_
    in turn.

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

    That would mean attr_accesor is in Module right?

    Please look at this IRB session:

    localhost:~ mkamp$ irb
    >> Module.private_methods.grep /attr/

    => ["attr", "attr_reader", "attr_writer", "attr_accessor"]
    >> Module.private_methods(false).grep /attr/

    => []
    >> Kernel.private_methods(false).grep /attr/

    => ["attr", "attr_reader", "attr_writer", "attr_accessor"]

    Why is that? After looking at the rdoc documentation I would have
    assumed that attr_accessor would be a private method of Module, not
    of Kernel.

    Any idea?

    Cheers,
    Mariano
    Mariano Kamp, Nov 12, 2006
    #1
    1. Advertising

  2. On Mon, Nov 13, 2006 at 06:16:05AM +0900, Mariano Kamp wrote:
    > On Nov 12, 2006, at 8:00 PM, Ross Bamford wrote:
    >
    > >>After looking at the rdoc documentation I would have assumed that
    > >>attr_accessor would be a private method of Module, not of Kernel.
    > >>

    > >
    > >It's because:
    > >
    > >Kernel.is_a?(Module)
    > ># => true
    > >
    > >and the attr* methods are instance methods on Module.

    >
    > Yes, I _believe_ I understood that. It is "used" there.
    > Maybe that is what I don't get ... Why doesn't the method show up in
    > Module?


    Module.is_a? Class # => true
    Class.private_instance_methods(false).include? "attr_reader" # => false
    Kernel.is_a? Module # => true
    Module.private_instance_methods(false).include? "attr_reader" # => true


    --
    Mauricio Fernandez - http://eigenclass.org - singular Ruby
    Mauricio Fernandez, Nov 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jim Cain
    Replies:
    0
    Views:
    84
    Jim Cain
    Jul 18, 2003
  2. David Garamond
    Replies:
    1
    Views:
    87
    Dan Doel
    Jan 25, 2004
  3. Jack, Paul
    Replies:
    2
    Views:
    100
    Robert Klemme
    Dec 16, 2004
  4. Markus Werner

    attr / attr_accessor

    Markus Werner, Dec 30, 2004, in forum: Ruby
    Replies:
    5
    Views:
    203
    Mark Hubbart
    Dec 31, 2004
  5. Wilson Bilkovich

    Catching attr_accessor with set_trace_func

    Wilson Bilkovich, Nov 16, 2006, in forum: Ruby
    Replies:
    3
    Views:
    96
    Wilson Bilkovich
    Nov 16, 2006
Loading...

Share This Page