Home > Articles > Programming > Ruby

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

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.”

  • + Share This
  • 🔖 Save To Your Account