All this talk about Ruby/Tk, particularly the discovery that it all
works out of the box on Mac OS 10.4 with X11, has prompted me to start
learning to use it. The Pickaxe, along with other documentation that
I’ve found, states that you can pass options to Tk methods using either
a block or a hash. However, it seems that the pack() method never calls
its associated block, and thus you have to use the hash form.
The first Ruby/Tk example in the Pickaxe, which is on p. 255 of the 2nd
ed., is this:
require ‘tk’
root = TkRoot.new { title “Ex1” }
TkLabel.new(root) do
text ‘Hello, world!’
pack { padx 15 ; pady 15; side ‘left’ }
end
Tk.mainloop
Notice that when you run this, there is no padding above or below the
text label, and when you resize the window, the label is anchored to
the top of the window rather than the left. In other words, the block
passed to pack() is never executed. But change that line to:
pack(‘padx’=>15, ‘pady’=>15, ‘side’=>‘left’)
and the padding is there, and the label anchors to the left of the
window.
Is this a bug or omission in Ruby/Tk? Or is pack() not meant to use a
block, and thus it is a bug in the Pickaxe example? I have verified
that this behavior is the same on the default Ruby install in Mac OS X
10.4.3, and in the Windows one-click installer 1.8.2-15.