calling setter method from initialize method

Discussion in 'Ruby' started by Le Wang, Aug 13, 2011.

  1. Le Wang

    Le Wang Guest

    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, Aug 13, 2011
    #1
    1. Advertising

  2. Le Wang

    Le Wang Guest

    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 <> 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
     
    Le Wang, Aug 13, 2011
    #2
    1. Advertising

  3. Le Wang

    Knut Lickert Guest

    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
     
    Knut Lickert, Aug 13, 2011
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?VHUtVGhhY2g=?=

    DataBinder setter method.

    =?Utf-8?B?VHUtVGhhY2g=?=, Mar 15, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    341
    =?Utf-8?B?VHUtVGhhY2g=?=
    Mar 15, 2005
  2. kaeli
    Replies:
    5
    Views:
    15,936
    Sudsy
    Jan 5, 2004
  3. Matt
    Replies:
    1
    Views:
    526
    Heiner Kücker
    Dec 4, 2004
  4. Martin Kess
    Replies:
    2
    Views:
    1,133
  5. Tapio Kelloniemi
    Replies:
    1
    Views:
    126
Loading...

Share This Page