Tk and instance variables (pickaxe book Ed 2, p260)

J

John Maclean

Chaps,

There's a small section of code from the second edition of the pickaxe book on p260.

require 'tk'

image1 = TkPhotoImage.new {file "img1.gif" }
image2 = TkPhotoImage.new {file "img2.gif" }

b = TkButton.new(@root) do # <---- This line
# more code
# not shown for sake of brevity
end
# more code
# more code

Am I right in saying that the line that creates the new button has an instance variable that will be used to define the main root "container"?
 
D

David Vallner

Chaps,

There's a small section of code from the second edition of the pickaxe = =20
book on p260.

require 'tk'

image1 =3D TkPhotoImage.new {file "img1.gif" }
image2 =3D TkPhotoImage.new {file "img2.gif" }

b =3D TkButton.new(@root) do # <---- This line
# more code
# not shown for sake of brevity
end
# more code
# more code
Am I right in saying that the line that creates the new button has an =20
instance variable that will be used to define the main root "container"= ?


If my (horrible) memory of the 15 minutes I've worked in Tk serves me =20
right for a change, there's a

@root =3D TkRoot.new

missing there somewhere. All other widgets should require a parent =20
container parameter for their constructor. TkRoot serves as the toplevel =
=20
container for a Tk interface and contains some plumbing related to the =20
event loop and such.

David Vallner
 
R

Ross Bamford

Chaps,

There's a small section of code from the second edition of the pickaxe
book on p260.

require 'tk'

image1 = TkPhotoImage.new {file "img1.gif" }
image2 = TkPhotoImage.new {file "img2.gif" }

b = TkButton.new(@root) do # <---- This line
# more code
# not shown for sake of brevity
end
# more code
# more code
Am I right in saying that the line that creates the new button has an
instance variable that will be used to define the main root "container"?

Yes, that is an instance var. However, it's unset, so the above line is
equivalent to:

b = TkButton.new(nil) do

or just:

b = TkButton.new do

As it says on p256:

"It's a good habit to specify the root explicitly, but you could
leave it out -- along with the options -- [...]"

So I guess it creates it implicitly in this case. Maybe it's a typo, or
maybe a (rather confusing IMO) way to demonstrate that the argument
carries the root element, or something. There's been discussion here
before about instance variables at the top level I think, search out the
archives. The following short script demonstrates a few points:

# find out about @foo
puts "Self is #<#{self.class}:#{self.object_id}> (#{self.inspect})"
# => Self is #<Object:-604402504> (main)

p instance_variables.member?('@foo') # => false

test = @foo # => nil
p instance_variables.member?('@foo') # => false

@foo = "test" # => "test"
p instance_variables.member?('@foo') # => true

p @foo # => "test"
p self.instance_variable_get:)@foo) # => "test"
p self.instance_eval { @foo } # => "test"

def foo
@foo
end
public :foo

p foo # => "test"
p self.foo # => "test"
p Array.foo # => nil
1.instance_eval { @foo = 'bar' }
p 1.foo # => "bar"

(P.s. that's definitely not an example to follow, more of a "Don't do it
this way!" thing ;))

Cheers,
 

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

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,480
Members
44,900
Latest member
Nell636132

Latest Threads

Top