Ruby: array slice question
I'm going through Ruby Koans as a way to learn Ruby. I'm having trouble understanding what the difference is between the two test cases in red, below, as to why the first is an empty array while the latter is nil. Seems to me like they should be the same (whichever result), as both having an offset beyond the last element of the array. Or is only one past the last element a special case?
array = [:peanut, :butter, :and, :jelly]
assert_equal [:peanut], array[0,1]
assert_equal [:peanut, :butter], array[0,2]
assert_equal [:and, :jelly], array[2,2]
assert_equal [:and, :jelly], array[2,20]
assert_equal , array[4,0]
assert_equal , array[4,100]
assert_equal nil, array[5,0]
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
~ Terry Pratchett in Nation
How to Ask Questions the Smart Way
(not affiliated with this site, but well worth reading)
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)