Block and Closure (Block and Closures)

xiaoxiao2021-03-06  61

http://www.ruby-cn.org/

See http://martinfowler.com/bliki/closures.html

Block: just a piece of code, equivalent to an anonymous function; Closure (Closures): a code, can be passed to other methods as parameters. Let's go back and look at the automatic card player. At some point, we need to deal with the interlocking machine and user interface: Many buttons for users to choose songs and control playback, we need to specify the corresponding time handled code, and Ruby Block is very suitable for doing this. Assume that the designer of the singer has provided us with a basic button class through the Ruby extension.

Bstart = button.new ("start")

BPAUSE = Button.New ("pause")

# ...

How to deal with the user after pressing the button? The Button class provides a ButtonPressed method that can be called when the button is pressed. So the easiest way to create the subscar class of Button and then implement the ButtonPressed method in each subclass.

Class StartButton

DEF Initialize

Super ("start") # invoke button's initialize

end

Def buttonpressed

# Do start actions ...

end

end

Bstart = startButton.new

There are two problems that will first produce a large number of subclasses, if Button changes, all subclasses need to be maintained; second, the acts that need to be executed after the button should not be indicated on the button, this is the singer. responsibility. We can use block to eliminate these problems

Class JukeboxButton

Def Initialize (Label, & Action)

Super (Label)

@Action = action

end

Def buttonpressed

@ Action.call (Self)

end

end

Bstart = jukeboxbutton.new ("start") {songlist.start}

Bpause = jukeboxbutton.new ("pause") {songlist.pause}

The key point of the above code is in JukeboxButton # initialize's second parameter, which has a & symbol in front of this parameter, which represents this parameter in Ruby, which will be converted to a proc object, and associated with corresponding variables . In this example we assign it to the instance variable @Action. When the callback method ButtonPressed is executed, we use method proc # call to call the corresponding block. What is we get after you have created a proc object? Very interesting, we get not only a string of code. The context environment associated with this Block definition (within the range): Self value, define its method, variable, constant, etc. A interesting place in Ruby is that even if the variables in the block definition is not within its use range, this variable remains, can still be used.

Let's take an example, this example uses the method Proc, this method changes a block to a proc object.

DEF NTIMES (ATHING) RETURN PROC {| N | ATHING * N} End P1 = NTIMES (23) P1.Call (3) »69 P1.Call (4)» 92 P2 = NTIMES ("Hello") p2.call 3) »" Hello Hello Hello "Method NTIMES Returns a PROC object, this object references the parameters of this function: athing. Even if this parameter is not in his own scope, this block can also access this parameter when the block is called. (,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,

转载请注明原文地址:https://www.9cbs.com/read-88512.html

New Post(0)