# An attr\_\* in a constructor?

**URL:** <https://www.ruby-forum.com/t/an-attr-in-a-constructor/119890>\
**Category:** Ruby\
**Created:** [November 12, 2007, 12:05am UTC](https://www.ruby-forum.com/t/an-attr-in-a-constructor/119890 "2007-11-12T00:05:43Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 12, 2007, 12:05am UTC](https://www.ruby-forum.com/t/an-attr-in-a-constructor/119890/1 "2007-11-12T00:05:43Z")

</div>

Hi,

Can I not use an attr\_\* in a constructor? I am receiving a

`initialize': undefined method`attr\_accessor’

`
class Whatever
  def initialize
    attr_accessor :log
    @log = 'whatever'
  end
end
Whatever.new
`

Thanks

Aidy

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 12, 2007, 12:30am UTC](https://www.ruby-forum.com/t/an-attr-in-a-constructor/119890/2 "2007-11-12T00:30:17Z")

</div>

On Nov 11, 2007 6:05 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> @log = 'whatever'
> 
> ```
> 
> end  
> end
> 
> Whatever.new

The attr\_\* methods are private instance methods in Module, which means  
that they are methods for modules and classes.

the initialize method is an instance method, in this case of the  
Whatever class. Try:

class Whatever  
attr\_accessor :log

def initialize  
self.log = ‘whatever’  
# or @log = ‘whatever’  
end  
end

–  
Rick DeNatale

My blog on Ruby  
[http://talklikeaduck.denhaven2.com/](http://talklikeaduck.denhaven2.com/)

---

<div class="post-metadata">

**Author:** ![aidy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/eada6e/32.png) [@aidy](https://www.ruby-forum.com/u/aidy)\
**Post date:** [November 12, 2007, 11:35am UTC](https://www.ruby-forum.com/t/an-attr-in-a-constructor/119890/3 "2007-11-12T11:35:38Z")

</div>

On 11 Nov, 23:28, Rick DeNatale [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> On Nov 11, 2007 6:05 PM, aidy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> self.log = 'whatever'
> # or @log = 'whatever'
> 
> ```
> 
> end  
> end
> 
> –  
> Rick DeNatale

Thanks Rick, I am having a little trouble with OO at the moment.

Aidy
