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

**URL:** <https://www.ruby-forum.com/t/ruby-tk-pack-with-block-doesnt-work/49230>\
**Category:** Ruby\
**Created:** [December 1, 2005, 5:34pm UTC](https://www.ruby-forum.com/t/ruby-tk-pack-with-block-doesnt-work/49230 "2005-12-01T17:34:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![doodpants](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@doodpants](https://www.ruby-forum.com/u/doodpants)\
**Post date:** [December 1, 2005, 5:34pm UTC](https://www.ruby-forum.com/t/ruby-tk-pack-with-block-doesnt-work/49230/1 "2005-12-01T17:34:34Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![doodpants](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@doodpants](https://www.ruby-forum.com/u/doodpants)\
**Post date:** [December 2, 2005, 8:35am UTC](https://www.ruby-forum.com/t/ruby-tk-pack-with-block-doesnt-work/49230/2 "2005-12-02T08:35:57Z")

</div>

From: “Karl von Laudermann” [removed\_email\_address@domain.invalid](mailto: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](mailto: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.

---

<div class="post-metadata">

**Author:** ![doodpants](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/58f4c7/32.png) [@doodpants](https://www.ruby-forum.com/u/doodpants)\
**Post date:** [December 2, 2005, 8:56am UTC](https://www.ruby-forum.com/t/ruby-tk-pack-with-block-doesnt-work/49230/3 "2005-12-02T08:56:15Z")

</div>

From: Hidetoshi NAGAI [removed\_email\_address@domain.invalid](mailto: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](mailto: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
