Recipe 4.1. Iterating Over an Array
Problem
You want to perform some operation on each item in an array.
Solution
Iterate over the array with
Enumerable#each. Put into a block the code you want to execute for each item in the array.
...
Recipe 4.2. Rearranging Values Without Using Temporary Variables
Problem
You want to rearrange a number of variables, or assign the elements of an array to individual variables.
Solution
Use a single assignment statement. Put the des...
Recipe 4.3. Stripping Duplicate Elements from an Array
Problem
You want to strip all
duplicate elements from an array, or prevent duplicate elements from being added in the first place.
Solution
Use Array#uniq to create a new arra...
Recipe 4.4. Reversing an Array
Problem
Your array is the wrong way around: the last item should be first and the first should be last.
Solution
Use reverse to create a new array with the items reversed. Internal
subarrays will not...
Recipe 4.5. Sorting an Array
Problem
You want to sort an array of objects, possibly according to some custom notion of what "sorting" means.
Solution
Homogeneous
arrays of common data types, like strings or numbers, can be sorted ...
Recipe 4.6. Ignoring Case When Sorting Strings
Problem
When you sort a list of strings, the strings beginning with uppercase letters sort before the strings beginning with lowercase letters.
list = ["Albania", "anteater", "zorilla", "Z...
Recipe 4.7. Making Sure a Sorted Array Stays Sorted
Problem
You want to make sure an array stays sorted, even as you replace its elements or add new elements to it.
Solution
Subclass Array and override the methods that add items to the ar...
Recipe 4.8. Summing the Items of an Array
Problem
You want to add together many objects in an array.
Solution
There are two good ways to accomplish this in Ruby. Plain vanilla iteration is a simple way to approach the problem:
co...
Recipe 4.9. Sorting an Array by Frequency of Appearance
Problem
You want to sort an array so that its least-frequently-appearing items come first.
Solution
Build a histogram of the frequencies of the objects in the array, then u...
Recipe 4.10. Shuffling an Array
Problem
You want to put the elements of an array in
random order.
Solution
The simplest way to shuffle an array (in Ruby 1.8 and above) is to sort it
randomly:
[1,2,3].sort_by { rand } # => [1,...
Recipe 4.11. Getting the N Smallest Items of an Array
Problem
You want to find the
smallest few items in an array, or the
largest, or the most extreme according to some other measure.
Solution
If you only need to find the singl...
Recipe 4.12. Building Up a Hash Using Injection
Problem
You want to create a hash
from the values in an array.
Solution
As seen in Recipe 4.8, the most straightforward way to solve this kind of problem is to use Enumerable#inject....
Recipe 4.13. Extracting Portions of Arrays
Problem
Given an array, you want to retrieve the elements of the array that occupy certain positions or have certain properties. You might to do this in a way that removes the matching elements
...
Recipe 4.14. Computing Set Operations on Arrays
Problem
You want to find the union, intersection, difference, or Cartesian product of two arrays, or the complement of a single array with respect to some universe.
Solution
Array objec...
AJAX popular information C# language guides Windows books and cookbooks
.......
С 2009 года мы стали переводить структура сайта на различные языки. Сайт теперь будет содержать книги не только на английском языке, но также и на других европейских языках, в том числе и на Русском языке.