Blocks as Closures (Part 3)

That’s where blocks come in, to simplify the definition and use of these buttons making maintenance simpler so we could call the specific block to which we pass on the necessary parameters or methods to attain the final outcome. Below is an example of how this can be achieved with blocks creating a class called devicebutton:

class DeviceButton < Button
def initialize(label, &process)
super(label)
@action = action
end
def buttonSelected
@action.call(self)
end
end

bPlay = DeviceButton.new(“Play”) {cdList.play}
bPause = DeviceButton.new(“Pause”) {cdList.pause}

This entry was posted in Development, Programming Basics and tagged , . Bookmark the permalink. Comments are closed, but you can leave a trackback: Trackback URL.