Recipe 7.1. Creating and Invoking a Block
Problem
You want to put some Ruby code into an object so you can pass it around and call it later.
Solution
By this time, you should familiar with a block as some Ruby code enclosed in c...
Recipe 7.2. Writing a Method That Accepts a Block
Problem
You want to write a method that can accept and call an attached code block: a method that works like Array#each, Fixnum#upto, and other built-in Ruby methods.
Solution
You don't ne...
Recipe 7.3. Binding a Block Argument to a Variable
Problem
You've written a method that takes a code block, but it's not enough for you to simply call the block with yield. You need to somehow bind the code block to a variable, so you can manip...
Recipe 7.4. Blocks as Closures: Using Outside Variables Within a Code Block
Problem
You want to share
variables
between a method, and a code block defined within it.
Solution
Just reference the
variables, and Ruby will ...
Recipe 7.5. Writing an Iterator Over a Data Structure
Problem
You've created a custom data structure, and you want to implement an each method for it, or you want to implement an unusual way of iterating over an existing data structure.
So...
Recipe 7.6. Changing the Way an Object Iterates
Problem
You want to use a data structure as an Enumerable, but the object's implementation of #each doesn't iterate the way you want. Since all of Enumerable's methods are based on each, this make...
Recipe 7.7. Writing Block Methods That Classify or Collect
Problem
The basic block methods that come with the Ruby standard library aren't enough for you. You want to define your own method that classifies the elements in an enumeration (like E...
Recipe 7.8. Stopping an Iteration
Problem
You want to interrupt an iteration from within the code block you passed into it.
Solution
The simplest way to interrupt execution is to use break. A break statement will jump out of the clos...
Recipe 7.9. Looping Through Multiple Iterables in Parallel
Problem
You want to traverse multiple
iteration methods simultaneously, probably to match up the corresponding elements in several different arrays.
Solution
The
SyncEn...
Recipe 7.10. Hiding Setup and Cleanup in a Block Method
Problem
You have a setup method that always needs to run before custom
code, or a cleanup method that needs to run afterwards. You don't trust the person writing the code (possibly...
AJAX popular information C# language guides Windows books and cookbooks
.......
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.