Created
April 25, 2012 05:14
-
-
Save jamesmanning/2486675 to your computer and use it in GitHub Desktop.
weird array slice behavior
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_slicing_arrays_is_weird | |
array = [:peanut, :butter, :and, :jelly] | |
assert_equal :peanut, array[0] | |
assert_equal :butter, array[1] | |
assert_equal :and, array[2] | |
assert_equal :jelly, array[3] | |
# this confirms that 4 is an invalid index into the array | |
assert_equal nil, array[4] | |
assert_equal [], array[0,0] | |
assert_equal [], array[1,0] | |
assert_equal [], array[2,0] | |
assert_equal [], array[3,0] | |
# wtf? 4 isn't a valid starting index, why do we get back an array here? | |
# However, it appears to be intentional based on the special cases listed here: | |
# http://www.ruby-doc.org/core-1.9.3/Array.html#method-i-slice | |
assert_equal [], array[4,0] | |
assert_equal nil, array[5,0] | |
assert_equal nil, array[6,0] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment