From: Kevin Smith Date: 2000-12-21T07:03:16+09:00 Subject: [ruby-talk:7800] Re: Regex array subscripts 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. I'm also a nuby, but here's my take: Just because you see [] doesn't mean you're looking at an array. something[whatever] might be accessing an item from an array, or from a hash, or a portion of a string. Basically, any class can define a [] operator and do whatever it wants with it. So "string"[0] returns "s", and "string"['r'] returns 'r'. There are many other variations. If you don't have the PR book yet, I highly recommend it. Kevin