In Ruby, the so-called block-based method refers to the abstraction of the control structure. It was initially designed to abstract loops, so it is also called an iterator.
Syntax: method_name do... end or method_name {}
1 # Block call
2 Def Foo
3 Print Yield ( 5 )
4 End
5
6 Foo { | A |
7 If A > 0
8 " Positive "
9 Elsif < 0
10 " Negative "
11 Else
12 " Zero "
13 End
14 }
15
16 Foo ()
17
18 # Use proc (process object) block call
19 Quux = Proc { | A |
20 If A > 0
21 " Positive "
22 Elsif < 0
23 " Negative "
24 Else
25 " Zero "
26 End
27 }
28
29 Def Foo (P)
30 Print P. Call ( 5 )
31 End
32
33 Foo (quux)
1 // Traditional Delegation
2 Public Delegate String Foodelegate ( Int A );
3 Public String Foo ( Int A)
4 {
5 If ( > 0 )
6 Return " Positive " ;
7 Else If ( < 0 )
8 Return " Negative " ;
9 Return " Zero " ;
10 }
11 Foodelegate myd1 = New Foodelegate (FOO );
12 Console. writeline (myd1 ( 5 ));
13
14 // Anonymous Delegation
15 Public Delegate String Foodelegate ( Int A );
16 Foodelegate myd1 = Delegate ( Int A)
17 {
18 If ( > 0 )
19 Return " Positive " ;
20 Else If ( < 0 )
21 Return " Negative " ;
22 Return " Zero " ;
23 }
24 Console. writeline (myd1 ( 5 ));