Block and Closure Function

Block and Closure Function​
 
Introduction
Lately, a friend of mine just told how awesome functional programming awesome was. One of its feature which catch my interest most is the closure function. In fact, I used this feature a lot in prototype programming (Javascript, Lua) and decided to research about this feature in Ruby. And well, we get block, proc and lambda there.
 
Block
Block is a cool feature in Ruby, and most of you are using this. We see this a lot when working with Array, this is an example of block:
 

array = [1, 2, 3, 4, 5]array.each { |x| puts x }array.each do |x| puts xendOf course, those above blocks are equal. And the question there, how we make our own blocks for class. Luckily, we have the powerful yield keyword to do our work there:

class Game_Party < Game_Unit def each_member members.each { |member| yield member } endendWith this short code, we can use block with Game_Party ($game_party):

$game_party.each_member { |member| puts member.name }$game_party.members.each { |member| puts member.name }Those 2 lines do the same thing, but in first case, you can control your way to work with each member, and it's a better convention than calling its property and mapping each member of that array.
 
For example of change the way to work with each member, we have a battle system, and each of end turn, we reset all actors ready state, except for actors who need to rest (because of some skills require them to rest for a few turns):

class Game_Party < Game_Unit def reset_state members.each do |member| member.reset_state # a method to reset for each member yield member if member.is_ready? # only return ready member in block end endendThat way, you can do anything with all members who don't need a rest in new turn:

$game_party.reset_state do |member| # do anything with member that don't need a rest # in short, only member has is_ready? method return trueend$game_party.members do |member| member.reset_state if member.is_ready? # do anything with member that don't need a rest endendThose two blocks do the same thing, but in case 1, we have a nicer convention, and we can use it as a normal method (reset_state for all members). In short, we can shorten our code in this way.
 
But wait, we remove the block, only use the method:

$game_party.reset_stateAnd get a crash! yield only work when a block is given, so we have to change the code a bit to check if the block is given:

class Game_Party < Game_Unit def reset_state members.each do |member| member.reset_state # a method to reset for each member end # separate mapping with yield for better code members.each do |member| yield member if member.is_ready? # only return ready member in block end if block_given? endendTry again, and it work beautifully! The method block_given? only return true if a block is given.
 
Those examples are good for now, but what if we have a block that use again and again all the time? Procs and Lambdas will do it for you in a nice and short way.

Proc (Procedure)

Sometimes, you will find that you write a block code over and over again, and you also want to pass the function around method(s) and class(es). Proc is the best way to do this. For creating a new Proc, you can just use below code:

func1 = Proc.new { |arg1, arg2| your_code_here() }func2 = proc { |arg1, arg2| your_code_here() }Those two lines do the same thing, except in ruby 1.8.x, the func2 will check number of arguments when it's called, whilst func1 doesn't. From ruby 1.9.x, func1 and func2 are exactly the same, they don't check number of arguments and will pass nil value to missing argument(s).

And to call the Proc, there are two ways to execute them:

func = Proc.new { |a, b| return a + b } # create procfunc.call(1, 2) # will return 3func[1, 2] # also return 3Lambda

Lambda does the same thing as Proc, except it will check number of argument on call.

# in this example, your_code_here() doesn't raise error when a and b are nillamb1 = lambda { |a, b| your_code_here() }lamb2 = ->(a, { your_code_here() }func = Proc.new { |a, b| your_code_here() }lamb1.call() # raises an errorlamb2.call() # raises an errorfunc.call() # does not raise errorlamb1 and lamb2 are the same, and syntax in lamb2 is called stabby lambda.

 
RESERVED FOR USAGE AND MORE INFORMATION!!!
Nice tutorial!

One question:

Wouldnt it be better if you would write the last code like this?

class Game_Party < Game_Unit def reset_state members.each do |member| member.reset_state # a method to reset for each member yield member if block_given? && member.is_ready? end endendThen you wouldnt have to iterate through members two times.

Or am I misunderstanding something?
In this case, they are the same, but in case which comdition is too complicated, separate them is a better way to make your code looks clean
Ok, thanks!
Ho it seem I saw this kind of code in moghunter script if I remember is like this i think....I use this method for make my command work in my title screen ( I think)

t

def dispose_commands
      @com.each {|sprite| sprite.dispose_sprites }
  end

But I never notice this was have a name~
Don't forget to cover the "stabby lambda;" it may be shorthand for a more formal lambda definition, but I use it constantly (probably due to my excessive use of REPLs these days). In addition to that, I'd recommend using proc over Proc.new (and there used to be subtle differences between the two in Ruby 1.8 which are probably worth covering). Oh, and I'd also mention that you absolutely cannot serialize any of these constructs, as they're inherently tied to the state of memory at the time they're executed; that can't be written to disk.


This was a great overview, by the way, especially since you showed how these things are relevant to RGSS3. Thank you for writing it.
Excellent post! Thanks for sharing.

@eugene222:

The two code snippets are functionally different..

In your example when the first ready member is yielded only members up to that point have been reset.

In the snippet given by Dr.Hikari all members have been reset when the first ready member is yielded.

This may not matter, but at the same time it could.

*hugs*

Original thread: https://forums.rpgmakerweb.com/threads/block-and-closure-function.25963/