Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Ruby (http://www.velocityreviews.com/forums/f66-ruby.html)
-   -   calling setter method from initialize method (http://www.velocityreviews.com/forums/t867547-calling-setter-method-from-initialize-method.html)

Le Wang 08-13-2011 04:46 PM

calling setter method from initialize method
 
Hi all,

Why doesn't aAttribute get set?

class AClass
attr_accessor :aAttribute, :bAttribute
def initialize()
aAttribute= 1
@bAttribute= 1
end
end

a= AClass.new
a.aAttribute # nil
a.bAttribute # 1

Le Wang 08-13-2011 06:41 PM

Re: calling setter method from initialize method
 
Sorry for the noise. I was in a a weird head space :), it should be
self.aAttribute.

On Aug 14, 12:46*am, Le Wang <lewang1980use...@gmail.com> wrote:
> Hi all,
>
> Why doesn't aAttribute get set?
>
> class AClass
> * attr_accessor :aAttribute, :bAttribute
> * def initialize()
> * * aAttribute= 1
> * * @bAttribute= 1
> * end
> end
>
> a= AClass.new
> a.aAttribute * * * * * * * * * # nil
> a.bAttribute * * * * * * * * * # 1



Knut Lickert 08-13-2011 06:44 PM

Re: calling setter method from initialize method
 
Am 13.08.2011 18:46, schrieb Le Wang:
> class AClass
> attr_accessor :aAttribute, :bAttribute
> def initialize()
> aAttribute= 1
> @bAttribute= 1
> end
> end
>
> a= AClass.new
> a.aAttribute # nil
> a.bAttribute # 1


With aAttribute= 1 you define a local variable aAttribute.

You have to use self.aAttribute= 1





All times are GMT. The time now is 07:09 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57