# Attr\_reader as a class method

**URL:** <https://rubytalk.org/t/attr-reader-as-a-class-method/28784>\
**Category:** ruby-talk\
**Created:** [9 July 2006 07:17 UTC](https://rubytalk.org/t/attr-reader-as-a-class-method/28784 "2006-07-09T07:17:59Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Minkoo](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Minkoo](https://rubytalk.org/u/Minkoo)\
**Post date:** [9 July 2006 07:17 UTC](https://rubytalk.org/t/attr-reader-as-a-class-method/28784/1 "2006-07-09T07:17:59Z")

</div>

Hi list.

I'd like to use attr\_reader to introduce a class method. For example,  
the following is the code I've tried.

irb(main):001:0\> class Foo  
irb(main):002:1\> ONE = 1  
irb(main):003:1\> end  
=\> 1  
irb(main):004:0\> class Foo  
irb(main):005:1\> attr\_reader :ONE  
irb(main):006:1\> end  
=\> nil  
irb(main):007:0\> Foo.ONE  
NoMethodError: undefined method `ONE' for Foo:Class  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from (irb):7  
irb(main):008:0\> class Foo  
irb(main):009:1\> class \<\< self  
irb(main):010:2\> attr\_reader :ONE  
irb(main):011:2\> end  
irb(main):012:1\> end  
=\> nil  
irb(main):013:0\> Foo.ONE  
=\> nil  
irb(main):014:0\>

As you can see, I've failed. But the following works:

irb(main):014:0\> class Foo  
irb(main):015:1\> def self.ONE  
irb(main):016:2\> return ONE  
irb(main):017:2\> end  
irb(main):018:1\> end  
=\> nil  
irb(main):019:0\> Foo.ONE  
=\> 1  
irb(main):020:0\>

How can I use attr\_reader to introduce a method I've written  
right above?

Sincerely,  
Minkoo Seo

> **···**
>
> from :0

---

<div class="post-metadata">

**Author:** ![Ryan\_Davis1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/ryan_davis1/32/1848_2.png) [@Ryan\_Davis1](https://rubytalk.org/u/Ryan_Davis1)\
**Post date:** [9 July 2006 07:24 UTC](https://rubytalk.org/t/attr-reader-as-a-class-method/28784/2 "2006-07-09T07:24:56Z")

</div>

The example is simply flawed. ONE is a constant and can be accessed from outside Foo as Foo.ONE without any extra code. attr\_\* don't provide access to constants, only instance variables (including the instance of a class).

> **···**
>
> On Jul 9, 2006, at 12:17 AM, Minkoo Seo wrote:
> 
> > I'd like to use attr\_reader to introduce a class method. For example,  
> > the following is the code I've tried.
> > 
> > irb(main):001:0\> class Foo  
> > irb(main):002:1\> ONE = 1  
> > irb(main):003:1\> end

---

<div class="post-metadata">

**Author:** ![Minkoo](https://avatars.discourse-cdn.com/v4/letter/m/db5fbb/32.png) [@Minkoo](https://rubytalk.org/u/Minkoo)\
**Post date:** [9 July 2006 07:32 UTC](https://rubytalk.org/t/attr-reader-as-a-class-method/28784/3 "2006-07-09T07:32:40Z")

</div>

I've made a mistake. I should've call Foo::ONE instead of Foo.ONE  
when accessing constants. By doing so, I don't have to make any  
methods.

Thanks.

Sincerely,  
Minkoo Seo

> **···**
>
> On 7/9/06, Ryan Davis \<ryand-ruby@zenspider.com\> wrote:
> 
> > On Jul 9, 2006, at 12:17 AM, Minkoo Seo wrote:
> > 
> > \> I'd like to use attr\_reader to introduce a class method. For example,  
> > \> the following is the code I've tried.  
> > \>  
> > \> irb(main):001:0\> class Foo  
> > \> irb(main):002:1\> ONE = 1  
> > \> irb(main):003:1\> end
> > 
> > The example is simply flawed. ONE is a constant and can be accessed  
> > from outside Foo as Foo.ONE without any extra code. attr\_\* don't  
> > provide access to constants, only instance variables (including the  
> > instance of a class).
