- Slicing an Array
- Iterating over an Array
- Creating Enumerable Classes
- Sorting an Array
- Iterating over Nested Arrays
- Modifying All the Values in an Array
- Sorting Nested Arrays
- Building a Hash from a Config File
- Sorting a Hash by Key or Value
- Eliminating Duplicate Data from Arrays (Sets)
- Working with Nested Sets
Building a Hash from a Config File
This is very similar to an earlier example in the section “Searching Strings,” in Chapter 2, “Working With Strings.” Here we are processing a simple format config file. This is a sample of what such a file looks like:
variable1 = foo variable2 = bar variable3 = baz
For the sake of simplicity, instead of a File for simulated input, this example uses a simple String with some \n (newline) separators.
In plain English, those inner lines mean, “Take the current line and call the #split on it, splitting on the ‘=’ character; pass each element of the resulting two-element Array in to the block; call the #strip method on the Strings to remove any whitespace, and return the modified Array to tmp_ary. Hash#store expects two parameters, not an Array, so we use the splat (*) operator to expand the tmp_ary Array down so that it appears to be a list of parameters.”