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

  • Thread starter Karl von Laudermann
  • Start date
K

Karl von Laudermann

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.
 
H

Hidetoshi NAGAI

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: said:
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.
 
H

Hidetoshi NAGAI

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: said:
'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
 

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

Similar Threads

Ruby TK 2
Bug with Ruby/Tk encoding (ruby-1.9.1-rc1) 6
Tk and Tile with Tk8.5 9
Ruby/Tk? 1
Ruby/Tk 10
ruby/tk : hide TkFrame 1
how to use Ruby / Tk to display a text message status box 15
Ruby/tk Help Please 19

Members online

No members online now.

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top