[LRUG] Array.slice question

Neil Middleton neil.middleton at gmail.com
Wed Sep 7 01:33:12 PDT 2011


You're asking for no length with [3,0] hence it slicing at position 3 for 0 chars. With the second version you're using an out of range start position hence the nil.

N 

Neil

On Wednesday, 7 September 2011 at 09:29, Michael Pavling wrote:

> I'm just running through the Ruby Koans for a bit of fun, and I've
> quickly come upon a case that doesn't make sense to me. I've re-read
> the documentation, but I still don't understand why it happens.
> 
> Having a play with this array in the console:
> >> x = %w(a b c)
>  => ["a", "b", "c"]
> >> x[1]
>  => "b"
> >> x[2]
>  => "c"
> >> x[3]
>  => nil
> 
> All makes sense so far, but when I call x[3,0] I *expect* it to return
> nil (as the starting index is out of range), but it returns an empty
> array:
> >> x[3,0]
>  => []
> 
> x[4,0] behaves as I expected:
> >> x[4,0]
>  => nil
> 
> I know it's going to be me not understanding the documentation
> properly, but *unless* I can understand it, it's going to leave me
> confused. Can anyone explain it simply and clearly, please?
> 
> TIA
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org (mailto:Chat at lists.lrug.org)
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.lrug.org/pipermail/chat-lrug.org/attachments/20110907/8c77aa33/attachment.html>


More information about the Chat mailing list