/    Sign up×
Community /Pin to ProfileBookmark

Ruby: array slice question

I’m going through [URL=”http://rubykoans.com/”]Ruby Koans[/URL] 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?

def test_slicing_arrays
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]
[COLOR=”#FF0000″]assert_equal [], array[4,0][/COLOR]
assert_equal [], array[4,100]
[COLOR=”#FF0000″]assert_equal nil, array[5,0][/COLOR]


to post a comment

0Be the first to comment 😎



Help @NogDog spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 11.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ

legal: ({
terms: of use,
privacy: policy
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
recent_tips: (
tipper: @bahaedd,
tipped: article
amount: 1000 SATS,

tipper: @Balmasexy,
tipped: article
amount: 1000 SATS,

tipper: @mbsaad,
tipped: article
amount: 1000 SATS,