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. Advertisements

  2. Le Wang

    Le Wang Guest

    Sorry for the noise. I was in a a weird head space :), it should be
    self.aAttribute.
     
    Le Wang, Aug 13, 2011
    #2
    1. Advertisements

  3. Le Wang

    Knut Lickert Guest

    Am 13.08.2011 18:46, schrieb Le Wang:
    With aAttribute= 1 you define a local variable aAttribute.

    You have to use self.aAttribute= 1
     
    Knut Lickert, Aug 13, 2011
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.