# Attr\_accessor/\_reader/\_writer and method\_defined?

**URL:** <https://rubytalk.org/t/attr-accessor-reader-writer-and-method-defined/9841>\
**Category:** ruby-talk\
**Created:** [25 January 2004 12:20 UTC](https://rubytalk.org/t/attr-accessor-reader-writer-and-method-defined/9841 "2004-01-25T12:20:16Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Garamond2](https://avatars.discourse-cdn.com/v4/letter/d/8dc957/32.png) [@David\_Garamond2](https://rubytalk.org/u/David_Garamond2)\
**Post date:** [25 January 2004 12:20 UTC](https://rubytalk.org/t/attr-accessor-reader-writer-and-method-defined/9841/1 "2004-01-25T12:20:16Z")

</div>

Why does a method defined using attr\_accessor/\_reader/\_writer is not  
found by .method\_defined? For example:

$ irb  
irb(main):001:0\> require ‘pp’  
=\> true  
irb(main):002:0\> PP.method\_defined? :pp  
=\> true  
irb(main):003:0\> PP.method\_defined? :sharing\_detection  
=\> false  
irb(main):004:0\> PP.respond\_to? :sharing\_detection  
=\> true

> **···**
>
> –  
> dave

---

<div class="post-metadata">

**Author:** ![Dan\_Doel](https://avatars.discourse-cdn.com/v4/letter/d/13edae/32.png) [@Dan\_Doel](https://rubytalk.org/u/Dan_Doel)\
**Post date:** [25 January 2004 12:40 UTC](https://rubytalk.org/t/attr-accessor-reader-writer-and-method-defined/9841/2 "2004-01-25T12:40:51Z")

</div>

My guess:

Taking a peek at pp.rb, #pp is defined for several things, including Kernel.  
Kernel methods get included in everything, which means everything gets an  
instance method #pp.

I’m guessing that #method\_defined? tells you if objects of the given Module  
have a given instance method. #sharing\_detection is an accessor for the  
class instance variable @sharing\_detection of PP, defined as:

class \<\< PP  
attr\_accessor :sharing\_detection  
end

Or some such. This means that the class in which the method is defined is  
the singleton class of PP, accessed by:

class \<\< PP; self; end

Sure enough, if you call method\_defined? on the singleton class, you get  
true.

So:

PP responds to #sharing\_detection, but it’s defined in its singleton class.  
PP defines a method #pp, so objects of class PP will respond to #pp.  
Kernel defines a method #pp so objects including Kernel will respond to #pp.

Hope this clears things up.

- Dan
