From: Bart Masschelein Date: 2005-08-31T03:53:02+09:00 Subject: Re: Tk Label and instance variables Oh, and of course I could get around with the following, but I would like to know why the instance variable appears to be not initialized inside the TkLabel initialization. value3 = @value TkLabel.new(root) do text "#{value3}" pack { padx 15 ; pady 15; side 'left' } end Bart Masschelein wrote: > Hello all, > > Below is a small piece of test code for a label using Tk. I try to set > a label using an instance variable, and one using a local variable. > The label with the local variable shows correctly in the window, the > one with the instance variable doesn't. I get a warning: instance > variable @value not initialized. Although the puts in between proves > differently. What is the magic between all of this? > > I tried adding value to attr_reader, but that didn't solve anything. > Not that I was expecting it to. > > As you probably can see, I come from a C/C++ background, and am > probably missing some Ruby fundamentals to solve this. My collection > of example scripts is rather small for the moment, and hence is also > my experience. So any directions are welcome. > > gr.b. > > > class LabelTest > def initialize(value) > @value = value > end > > def run > root = TkRoot.new { title "Label test" } > puts @value > TkLabel.new(root) do > text "#{@value}" > pack { padx 15 ; pady 15; side 'left' } > end > value2 = 200 > TkLabel.new(root) do > text "#{value2}" > pack { padx 15 ; pady 15; side 'left' } > end Tk.mainloop > end > end > > test = LabelTest.new(100) > test.run > > > >