Blocks or simply chunks of code as in the last example may be existing variables which changes after that same variable passes thought the block, giving it a new value. Blocks may also be used to return a needed value to the method as in the last example, the resulting value of the last expression [...]
Tag Archives: Blocks
Blocks for Transactions Discussed (Part 2)
After it goes through the block, the file is closed taking with it the results of the block’s transactional process. This is something that should be done with all programming languages, to have the code have their own built-in file handling system which ensures that a file once opened is closed upon exit to avoid [...]
Blocks for Transactions Discussed (Part 1)
The sample code above shows the number of techniques that can be used to maximize the use of blocks for transactional control where the takeAndDosomething method is considered a class method and that it can be called independent or without influence of any single File Object. We used it to function in the same manner [...]
Blocks as Closures (Part 3)
That’s where blocks come in, to simplify the definition and use of these buttons making maintenance simpler so we could call the specific block to which we pass on the necessary parameters or methods to attain the final outcome. Below is an example of how this can be achieved with blocks creating a class called [...]
Blocks as Closures (Part 2)
Sample Code: class PlayButton < Button def initialize super(“Play”) # invoke Button’s initialize process end def buttonSelected # do something that should be done when the start button is pressed end end bPlay = PlayButton.new This type of approach would bring about two very problematic scenarios; one, the actions that are to be done when [...]
Blocks as Closures (Part 1)
As it turns out, blocks are the ideal tool for use ion implementing user interfaces where we have a defined graphic that signifies a button and pressing that button would result in a pre-set series of processes. A simple button definition can be: bPlay = Button.new(“Play”) bPause = Button.new(“Pause”) # so on and so forth…… [...]
Blocks for Transactions
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 [...]
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