str[regexp] is actually a method of the String class, you can find it here http://www.ruby-doc.org/core/classes/String.html#M001128
The second argument 1 will return the text corresponding to the first subpattern #coding[:=].*\r?\n , another example for your better understanding:
"ab123baab"[/(\d+)(ba+).*/, 0] # returns "123baab", since it is the complete matched text, ,0 can be omitted also "ab123baab"[/(\d+)(ba+).*/, 1] # returns "123", since the first subpattern is (\d+) "ab123baab"[/(\d+)(ba+).*/, 2] # returns "baa", since the second subpattern is (ba+)
source share