From: Robert Read Date: 2000-12-21T05:25:01+09:00 Subject: [ruby-talk:7792] Re: Regex array subscripts I'm still a nuby, but I'll give this shot. I believe regular expressions are treated differently when they are used for indexing arrays. So this: s[/\d+/] returns the first match in s, and the return value of /\d+/ is not really known or important. This a =~ /\d+/ returns the first index of the matched string in s. Perhaps another way of looking at it is regexps don't have return values, because the expression a regexp is used in has the return value. robert On Thu, Dec 21, 2000 at 05:06:44AM +0900, craig duncan wrote: > I'm just a Ruby-nuby and sparse documentation has me trying to construct > my own (deeper) explanations for why things work the way they do. wrt: > > "one 123 two"[/\d+/] => "123" > > even more simply we have: > > "one 123 two"['e 1'] => "e 1" > > So, what is confusing is when the documentation says: > > "Arrays are ordered, integer-indexed collections of any object." > > It appears that an integer index is only one way of accessing array > elements. Which is what had me confused, because i was led to think > that /\d/ and /\d+/ both return an (the same) integer. But apparently > they return the matched string, which works just fine as an array > index. Wierd.