Pickaxe Question

On page 53 of the Pickaxe Book there is a little example illustrating
Proc parameters:

songlist = SongList.new
class JukeboxButton < Button
def initialize(label, &action)
super(label)
@action = action
end
def button_pressed
@action.call(self)
end
end
start_button = JukeboxButton.new(“Start”) { songlist.start }
pause_button = JukeboxButton.new(“Pause”) { songlist.pause }

If I understand this example correctly, we are supposed to assume
that the
mapping of the instances of JukeboxButton to actual hardware buttons
happens, by magic, when we call the button initializer and pass it
the appropriate string with the super(label) message. But if that’s
the case, I don’t understand why button_pressed needs to pass self as
an argument to the @action.call message. It seems like the
songlist.start and songlist.pause methods just drop the argument.

On Aug 12, 2006, at 14:21, Chris G. wrote:

If I understand this example correctly, we are supposed to assume
that the
mapping of the instances of JukeboxButton to actual hardware
buttons happens, by magic, when we call the button initializer and
pass it the appropriate string with the super(label) message. But
if that’s the case, I don’t understand why button_pressed needs to
pass self as an argument to the @action.call message. It seems like
the songlist.start and songlist.pause methods just drop the argument.

Yeah, that’s true. I don’t need to pass it in: it was there from the
original example, where the block could interrogate the state of the
button. Sorry it was confusing.

Dave

On Aug 12, 2006, at 12:41 PM, Dave T. wrote:

Yeah, that’s true. I don’t need to pass it in: it was there from
the original example, where the block could interrogate the state
of the button. Sorry it was confusing.

Thanks for the quick reply, Dave. I’ve been using Ruby off and on for
awhile now but I never felt that i dug in and really understood it.
Hence the picky questions.


Neither a man nor a crowd nor a nation can be trusted to act humanely
or to think sanely under the influence of a great fear.

-Bertrand Russell, philosopher, mathematician, author, Nobel laureate
(1872-1970)

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs