[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

an attr_* in a constructor?

aidy

11/11/2007 11:00:00 PM

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

2 Answers

Rick DeNatale

11/11/2007 11:29:00 PM

0

On Nov 11, 2007 6:05 PM, aidy <aidy.rutter@gmail.com> wrote:
> 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



--
Rick DeNatale

My blog on Ruby
http://talklikeaduck.denh...

aidy

11/12/2007 10:32:00 AM

0

On 11 Nov, 23:28, Rick DeNatale <rick.denat...@gmail.com> wrote:
> On Nov 11, 2007 6:05 PM, aidy <aidy.rut...@gmail.com> 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
>
> --
> Rick DeNatale
>

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

Aidy