an attr_* in a constructor?

A

aidy

Hi,

Can I not use an attr_* in a constructor? I am receiving a

`initialize': undefined method `attr_accessor'

<CODE>
class Whatever
def initialize
attr_accessor :log
@log = 'whatever'
end
end

Whatever.new
</CODE>

Thanks

Aidy
 
R

Rick DeNatale

Hi,

Can I not use an attr_* in a constructor? I am receiving a

`initialize': undefined method `attr_accessor'

<CODE>
class Whatever
def initialize
attr_accessor :log
@log = 'whatever'
end
end

Whatever.new
</CODE>

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
 
A

aidy

On Nov 11, 2007 6:05 PM, aidy <[email protected]> wrote:
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

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

Aidy
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top