C
Chris Gehlker
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.
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.