Tuesday, October 1, 2013

Ruby - blocks, callbacks

BLOCKS:

http://stackoverflow.com/questions/814739/whats-this-block-in-ruby-and-how-does-it-get-passes-in-a-method-here
BLOCKS are a fairly basic part of ruby. They're delimited by either:
 do |arg0,arg1| ... end or
 { |arg0,arg1,arg2| ... }.
They allow you to specify a CALLBACK to pass to a METHOD.
This CALLBACK can be invoked in two ways - either by capturing it by specifying a final ARGUMENT prefixed with &, or by using the yield keyword 
CALLBACKS:

Idiomatic & Non-idiomatic ways of having CALLBACKS in RUBY:
http://stackoverflow.com/questions/1677861/how-to-implement-a-callback-in-ruby
The idiomatic approach would be to pass a BLOCK instead of a REFERENCE to a METHOD. One advantage a BLOCK has over a freestanding METHOD is CONTEXT - a BLOCK is a CLOSURE, so it can refer to variables from the SCOPE in which it was declared. This cuts down on the number of PARAMETERS do_stuff needs to pass to the --CALLBACK.

No comments:

Post a Comment

Followers

Blog Archive