Came out fine for me, and was highly educational! :)<br><br><div class="gmail_quote">On 7 September 2011 10:22, William Hamilton <span dir="ltr"><<a href="mailto:whamilton42@hotmail.com">whamilton42@hotmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<div><div dir="ltr"><div class="im">
<div>> > > Having a play with this array in the console:<br>> > > >> x = %w(a b c)[...]<br>> > All makes sense so far, but when I call x[3,0] I *expect* it to return<br>> > nil (as the starting index is out of range), but it returns an empty<br>
> > array:<br>> > >> x[3,0]<br>> > => []<br>> The key point is that the 3 in a[3] and the 3 in a[3,0] are not referring to the same thing. <br>> a[3] is directly accessing an element within the array. a[3,0] is accessing a portion of the array. This diagram should help (if the formatting makes it through.. copy and paste to fixed-width if it doesn't):<br>
> <br>> 0 1 2 3-------------| a | b | c |------------- 0 1 2<br>> <br>> Single-integer indexes are at the bottom. They access *elements* of the array.Double-integer indexes are at the top. They access *portions* of the array.<br>
> You can see that, technically, starting at the top 3 (which a[3,0] does) and counting forwards 0 is still within the array.<br>> <br>> I think the *reason* Ruby behaves like this for arrays is because it's beneficial for Strings (they share the same underlying code, of course). For example, it's totally valid to access the end portion of a String, which is basically the '' portion just after the final character. If you wanted to insert a character at the end of a string, you could do s[3,0] = 'd'.<br>
> <br>> - Will Hamilton<br></div><div><br></div><div><br></div><div><br></div></div><div>My poor diagram!</div><div><br></div><div>Try this: <a href="http://i.imgur.com/I4x96.png" target="_blank">http://i.imgur.com/I4x96.png</a></div>
<div><br></div><div><br></div><div>- Will Hamilton</div> </div></div>
<br>_______________________________________________<br>
Chat mailing list<br>
<a href="mailto:Chat@lists.lrug.org">Chat@lists.lrug.org</a><br>
<a href="http://lists.lrug.org/listinfo.cgi/chat-lrug.org" target="_blank">http://lists.lrug.org/listinfo.cgi/chat-lrug.org</a><br>
<br></blockquote></div><br>