Hello, I am noodling with the Ruby Koans and I ran across an Array slice behavior that I just can''t quite wrap my brain around: ruby-1.9.2-p180 :102 > array = [:peanut, :butter, :and, :jelly] => [:peanut, :butter, :and, :jelly] ruby-1.9.2-p180 :103 > array[4,0] => [] ruby-1.9.2-p180 :104 > array[5,0] => nil from http://www.ruby-doc.org/core/classes/Array.html#M000267 "Returns nil if the index (or starting index) are out of range." I don''t understand why the 4th element is an empty array, whereas the 5th element is out of range. I would expect the 4th element to be out of range as ''array'' contains elements 0-3. Can someone plz explain? Thanks! -Tk -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 11 Aug 2011, at 17:03, viciousfish wrote:> I am noodling with the Ruby Koans and I ran across an Array slice > behavior that I just can''t quite wrap my brain around: > > ruby-1.9.2-p180 :102 > array = [:peanut, :butter, :and, :jelly] > => [:peanut, :butter, :and, :jelly] > ruby-1.9.2-p180 :103 > array[4,0] > => [] > ruby-1.9.2-p180 :104 > array[5,0] > => nil > > from http://www.ruby-doc.org/core/classes/Array.html#M000267 > "Returns nil if the index (or starting index) are out of range." > > I don''t understand why the 4th element is an empty array, whereas the > 5th element is out of range. I would expect the 4th element to be out > of range as ''array'' contains elements 0-3.You''re right -- this behaviour is a bit strange, and doesn''t seem to match that documentation. I was about to say this looks like a bug in Ruby, but then I checked the 3rd edition of Programming Ruby, which has this to say about the #slice method: "Returns nil if the index of the first element selected is greater than the array size. If the start index equals the array size and a length or range parameter is given, an empty array is returned." This matches the behaviour you see, so it seems somebody is at least aware of this, and it''s perhaps even intentional. Might be worth posting the query to the Ruby mailing list, to see if anyone knows why #slice is designed this way. Chris -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
viciousfish wrote in post #1016211:> Hello, > > I am noodling with the Ruby Koans and I ran across an Array slice > behavior that I just can''t quite wrap my brain around: > > ruby-1.9.2-p180 :102 > array = [:peanut, :butter, :and, :jelly] > => [:peanut, :butter, :and, :jelly] > ruby-1.9.2-p180 :103 > array[4,0] > => [] > ruby-1.9.2-p180 :104 > array[5,0] > => nil > > from http://www.ruby-doc.org/core/classes/Array.html#M000267 > "Returns nil if the index (or starting index) are out of range." > > I don''t understand why the 4th element is an empty array, whereas the > 5th element is out of range. I would expect the 4th element to be out > of range as ''array'' contains elements 0-3. > > Can someone plz explain? >Slices are different than single indexes: array[4, 0] and array[4] point to two different spots in the array, and the spot array[4,0] points to is just inbounds, while array[4] is out of bounds. See here: http://www.ruby-forum.com/topic/1393096 -- Posted via http://www.ruby-forum.com/. -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
On 12 Aug 2011, at 01:07, 7stud -- wrote:> viciousfish wrote in post #1016211: > >> I am noodling with the Ruby Koans and I ran across an Array slice >> behavior that I just can''t quite wrap my brain around: >> >> ruby-1.9.2-p180 :102 > array = [:peanut, :butter, :and, :jelly] >> => [:peanut, :butter, :and, :jelly] >> ruby-1.9.2-p180 :103 > array[4,0] >> => [] >> ruby-1.9.2-p180 :104 > array[5,0] >> => nil >> >> from http://www.ruby-doc.org/core/classes/Array.html#M000267 >> "Returns nil if the index (or starting index) are out of range." >> >> I don''t understand why the 4th element is an empty array, whereas the >> 5th element is out of range. I would expect the 4th element to be out >> of range as ''array'' contains elements 0-3. > > Slices are different than single indexes: array[4, 0] and array[4] point > to two different spots in the array, and the spot array[4,0] points to > is just inbounds, while array[4] is out of bounds. See here: > > http://www.ruby-forum.com/topic/1393096Thanks for the link -- a very useful explanation, and I''d somehow managed to miss this point despite years of Rubying! I wonder if it might be worth a little patch to the Ruby API docs to clarify the semantics of array range access. May take this conversation to the Ruby list. Chris -- You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.