What exactly is the line row[/\w+/] doing in the following situation?
fields = rows.shift.map{ |row|
row[/\w+/]
}
I understand that the first value is removed from rows, its values are all passed into the block and replaced by the value returned from the block. But what is [/\w+/] doing to the value? In this case each row is an array of alphanumeric strings.
You can see how the string index operator
[]works here.For example, if you pass a number (
row[5]) it returns a substring containing the character at position 5 (0 being the first). But when a regular expression is used instead of a number, it returns the first substring that matches the regex.In this particular case the regex is matching one or more word characters A-Z, a-z, 0-9, and _