Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7026963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:11:10+00:00 2026-05-28T00:11:10+00:00

Possible Duplicate: Array slicing in Ruby: looking for explanation for illogical behaviour (taken from

  • 0

Possible Duplicate:
Array slicing in Ruby: looking for explanation for illogical behaviour (taken from Rubykoans.com)

In one of the Ruby koans, there’s the following problem:

def test_slicing_arrays
  array = [:peanut, :butter, :and, :jelly]

  assert_equal _, array[0,1]
  assert_equal _, array[0,2]
  assert_equal _, array[2,2]
  assert_equal _, array[2,20]
  assert_equal _, array[4,0]
  assert_equal _, array[4,100]
  assert_equal _, array[5,0]
end

You must fill in the _ with the correct statement. The first four asserts work how I’d expect them to, but I’m confused about the last three.

array[4,0] gives back [], as does array[4,100]. At this point I figured that ranges outside of the array (greater than 3 in this case) simply return an empty array.

But array[5,0] returns nil which has now confused me completely.

Can anyone explain this behaviour?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T00:11:11+00:00Added an answer on May 28, 2026 at 12:11 am

    Ruby is not a static language, forcing you to pre-declare the size of the array. It expands arrays as you assign to a particular element.

    Normally we’d append to the end of the array:

    array = []       # => []
    array << 1       # => [1]
    array += [2]     # => [1, 2]
    array.push(3)    # => [1, 2, 3]
    

    Or push onto the front of it:

    array.unshift(0) # => [0, 1, 2, 3]
    

    to add elements, which keeps the array accumulating the values without gaps.

    We can do it randomly too, which can be useful:

    array[10] = 10 # => 10
    array          # => [0, 1, 2, 3, nil, nil, nil, nil, nil, nil, 10]
    

    And that’s what you’ve encountered.


    You can predefine the array to a size, but it remains dynamic:

    ary = Array.new(10, nil) # => [nil, nil, nil, nil, nil, nil, nil, nil, nil, nil]
    ary[0] = 0               # => 0
    ary[10] = 10             # => 10
    ary                      # => [0, nil, nil, nil, nil, nil, nil, nil, nil, nil, 10]
    ary[12]=12               # => 12
    ary                      # => [0, nil, nil, nil, nil, nil, nil, nil, nil, nil, 10, nil, 12]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Array slicing in Ruby: looking for explanation for illogical behaviour (taken from
Possible Duplicate: Array slicing in Ruby: looking for explanation for illogical behaviour (taken from
Possible Duplicate: How to delete an element from an array in php? For instance,
Possible Duplicate: How do I generate a hashcode from a byte array in c#
Possible Duplicate: generating random numbers from skewed normal distribution i expect a long array
Possible Duplicate: JSON Array iteration in Android/Java I am fetching JSON string from server
Possible Duplicate: How to create comma separated list from array in PHP? I have
Possible Duplicate: Sort strings and numbers in Ruby I have an array of place
Possible Duplicate: Array#each vs. Array#map ruby-1.9.2-p180 :006 > ary = [a, b] => [a,
Possible Duplicate: Array size too big - ruby Sorry if this has been asked,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.