Ruby/Tk: pack() with block doesn't work

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.

From: “Karl von Laudermann” [email protected]
Subject: Ruby/Tk: pack() with block doesn’t work
Date: Fri, 2 Dec 2005 01:32:31 +0900
Message-ID: [email protected]

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.

‘pack’ method doesn’t accept a block.
So, I think that it is a bug in the Pickaxe example.

From: Hidetoshi NAGAI [email protected]
Subject: Re: Ruby/Tk: pack() with block doesn’t work
Date: Fri, 2 Dec 2005 16:31:20 +0900
Message-ID: [email protected]

‘pack’ method doesn’t accept a block.
So, I think that it is a bug in the Pickaxe example.

If use the following definition, ‘pack’ can accept a block. :wink:

class TkWindow
alias _pack pack
def pack(h=nil, &b)
_pack(h)

if b
  dummy_obj = Object.new
  dummy_obj.instance_variable_set('@w', self)
  class << dummy_obj
    def method_missing(id, val)
      @w.pack(id=>val)
    end
  end
  dummy_obj.instance_eval(&b)
end

self

end
end