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

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
doodpants (Guest)
on 2005-12-01 18:34
(Received via mailing list)
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.
nagai (Guest)
on 2005-12-02 09:35
(Received via mailing list)
From: "Karl von Laudermann" <removed_email_address@domain.invalid>
Subject: Ruby/Tk: pack() with block doesn't work
Date: Fri, 2 Dec 2005 01:32:31 +0900
Message-ID: <removed_email_address@domain.invalid>
> 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.
nagai (Guest)
on 2005-12-02 09:56
(Received via mailing list)
From: Hidetoshi NAGAI <removed_email_address@domain.invalid>
Subject: Re: Ruby/Tk: pack() with block doesn't work
Date: Fri, 2 Dec 2005 16:31:20 +0900
Message-ID: <removed_email_address@domain.invalid>
> '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. ;-)
-----------------------------------------------------------
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
This topic is locked and can not be replied to.