{"id":13082,"date":"2025-08-04T07:03:18","date_gmt":"2025-08-04T07:03:18","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=13082"},"modified":"2025-08-04T07:03:18","modified_gmt":"2025-08-04T07:03:18","slug":"ruby-koans-empty-array","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/ruby-on-rails\/ruby-koans-empty-array","title":{"rendered":"Ruby Koans: Why I Get Empty Array?"},"content":{"rendered":"<p>If you\u2019ve ever played with <a href=\"http:\/\/rubykoans.com\/\" target=\"_blank\" rel=\"noopener\">Ruby Koans<\/a>, you may have stumbled upon this puzzling behavior:<\/p>\n<p><strong>ruby<\/strong><br \/>\n<code>array = [:peanut, :butter, :and, :jelly]<\/p>\n<p>array[4, 0]    # => []<br \/>\narray[4, 100]  # => []<br \/>\narray[5, 0]    # => nil<\/code><\/p>\n<p>At first glance, this might seem inconsistent. Why do some of these return an empty array, and one returns nil?<\/p>\n<p>Let\u2019s break it down.<\/p>\n<h2>The Basics of Ruby Slicing<\/h2>\n<p>Ruby\u2019s Array#[] method accepts two arguments: start_index and length.<\/p>\n<p><strong>ruby<br \/>\nCopyEdit<\/strong><br \/>\n<code>array[start, length]<\/code><\/p>\n<ul>\n<li>If start is <strong>within bounds<\/strong> (i.e., 0 <= start <= array.size), Ruby returns a sub-array\u2014even if length is 0.<\/li>\n<li>If start is <strong>outside the bounds<\/strong> (i.e., start > array.size), Ruby returns nil.<\/li>\n<\/ul>\n<h2>Let&#8217;s Translate That<\/h2>\n<p>Here\u2019s what happens in our example:<\/p>\n<p><strong>ruby<br \/>\nCopyEdit<\/strong><\/p>\n<p><code>array = [:peanut, :butter, :and, :jelly]<br \/>\n# Indexes:  0        1         2     3<br \/>\n# Length:   4<\/code><br \/>\n<code>array[4, 0]<\/code><\/p>\n<ul>\n<li>The index 4 is exactly at the end of the array (array.length == 4).<\/li>\n<li>Ruby sees this as a valid &#8220;empty&#8221; slice at the end, so it returns an empty array: [].<\/li>\n<\/ul>\n<p><code>array[4, 100]<\/code><\/p>\n<ul>\n<li>Again, index 4 is valid.<\/li>\n<li>Ruby tries to take 100 elements from index 4, but since it\u2019s at the end, there&#8217;s nothing to return.<\/li>\n<li>Still valid, just empty \u2192 [].<\/li>\n<\/ul>\n<p><code>array[5, 0]<\/code><\/p>\n<ul>\n<li>Index 5 is <strong>beyond the end.<\/strong><\/li>\n<li>Ruby returns nil because that index doesn\u2019t exist at all.<\/li>\n<\/ul>\n<div class=\"qanda-read-box\"><div class=\"bg-light read-more-icon\"><img decoding=\"async\" src=\"https:\/\/assets.bacancytechnology.com\/qanda\/wp-content\/uploads\/2025\/04\/24061434\/read-txt.png\" alt=\"Also Read\"><p><\/p><h3>Also Read:<\/h3><a href=\"https:\/\/www.bacancytechnology.com\/blog\/ruby-on-rails-gems\" target=\"_blank\">Best Ruby On Rails Gems<\/a><\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>If you\u2019ve ever played with Ruby Koans, you may have stumbled upon this puzzling behavior: ruby array = [:peanut, :butter, :and, :jelly] array[4, 0] # => [] array[4, 100] # => [] array[5, 0] # => nil At first glance, this might seem inconsistent. Why do some of these return an empty array, and one [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":13083,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[11],"tags":[],"class_list":["post-13082","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-on-rails"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13082"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=13082"}],"version-history":[{"count":1,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13082\/revisions"}],"predecessor-version":[{"id":13084,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/13082\/revisions\/13084"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/13083"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=13082"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=13082"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=13082"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}