<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Tahoma
}
--></style>
</head>
<body class='hmmessage'><div dir='ltr'>
<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>My poor diagram!</div><div><br></div><div>Try this: http://i.imgur.com/I4x96.png</div><div><br></div><div><br></div><div>- Will Hamilton</div> </div></body>
</html>