Blocks or chunks of code that can be used for transactional control which in our example would have it open a file, process it then make sure that same file is closed after processing before exiting the code. There is an automatic function that ensures this and we will discuss it further in the following [...]
Monthly Archives: March 2008
Blocks for Transactions
Blocks and Iterators (Part 2)
Anytime the “yield” statement is called upon it calls on the chunk of code within the block executing it returning control to where the yield statement was called continuing on its way till the process is finished. That’s where the magic of Ruby is, that a block can be made to function like a method [...]
Blocks and Iterators (Part 1)
An iterator is a method of calling on a block of code which is somewhat similar to C, Java or Perl, somewhat. The usage of iterators in Ruby does something different as opposed to other languages that share similar functions. Initially a block normally appears adjacent a method or call, that call statement is written [...]
Blocks – Example
Below, we find sample code which uses block to return the values of the Fibonacci number Series. def fibMax(max) a1, a2 =1,1 # variables that are assigned parallel values while a1
Constructing Blocks
Blocks are code contained within the braces that if used with the last post’s example would be associated with the call to the function threeTimes. Within the defined method a call for yield is called three times in a row wherein each time a call to the block is sent the greeting is displayed which [...]