[LRUG] Array.slice question
James Cumming
james.cumming at yahoo.co.uk
Wed Sep 7 02:55:35 PDT 2011
Thanks, great explanation!
James Cumming, CFA
+44 7799 554468
________________________________
From: William Hamilton <whamilton42 at hotmail.com>
To: chat at lists.lrug.org
Sent: Wednesday, 7 September 2011, 10:17
Subject: Re: [LRUG] Array.slice question
> > Having a play with this array in the console:
> > >> x = %w(a b c)
[...]
> 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]
> => []
The key point is that the 3 in a[3] and the 3 in a[3,0] are not referring to the same thing.
a[3] is directly accessing an element within the array. a[3,0] is accessing a portion of the array. This diagram should help (if the formatting makes it through.. copy and paste to fixed-width if it doesn't):
0 1 2 3
-------------
| a | b | c |
-------------
0 1 2
Single-integer indexes are at the bottom. They access *elements* of the array.
Double-integer indexes are at the top. They access *portions* of the array.
You can see that, technically, starting at the top 3 (which a[3,0] does) and counting forwards 0 is still within the array.
I think the *reason* Ruby behaves like this for arrays is because it's beneficial for Strings (they share the same underlying code, of course). For example, it's totally valid to access the end portion of a String, which is basically the '' portion just after the final character. If you wanted to insert a character at the end of a string, you could do s[3,0] = 'd'.
- Will Hamilton
> From: chat-request at lists.lrug.org
> Subject: Chat Digest, Vol 68, Issue 10
> To: chat at lists.lrug.org
> Date: Wed, 7 Sep 2011 01:45:49 -0700
>
> Send Chat mailing list submissions to
> chat at lists.lrug.org
>
> To subscribe or unsubscribe via the World Wide Web, visit
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> or, via email, send a message with subject or body 'help' to
> chat-request at lists.lrug.org
>
> You can reach the person managing the list at
> chat-owner at lists.lrug.org
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Chat digest..."
>
>
> Today's Topics:
>
> 1. Re: Ru3y Manor! (Hakan Ensari)
> 2. Array.slice question (Michael Pavling)
> 3. Re: Array.slice question (Neil Middleton)
> 4. Re: Array.slice question (Michael Pavling)
> 5. Re: Array.slice question (Neil Middleton)
> 6. Re: Array.slice question (Tom Stuart)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Tue, 6 Sep 2011 22:07:41 +0100
> From: Hakan Ensari <hakan.ensari at papercavalier.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: Re: [LRUG] Ru3y Manor!
> Message-ID: <248F96C4685748D2A83CE4D907A6ACFC at papercavalier.com>
> Content-Type: text/plain; charset="utf-8"
>
> Yes, of course.
>
> Time is not my forte. Sorry for cluttering the inbox.
>
> --
> Hakan Ensari
> http://hakanensari.com
>
> On Tuesday, 6 September 2011 at 21:51, Leonardo Cassarani wrote:
>
> > It's a whole month after the start of RubyConf, actually (September 29th - October 1st).
> >
> > On 6 September 2011 21:48, Hakan Ensari <hakan.ensari at papercavalier.com (mailto:hakan.ensari at papercavalier.com)> wrote:
> > > Gents,
> > >
> > > If I'm not mistaken, that's overlapping with RubyConf?
> > >
> > > --
> > > Hakan Ensari
> > > http://hakanensari.com
> > >
> > > On Tuesday, 6 September 2011 at 10:56, James Adam wrote:
> > >
> > > > One and all,
> > > >
> > > > Rejoice! The Guvner will be opening the doors to the Ruby Manor once more:
> > > >
> > > > http://rubymanor.org/3/
> > > >
> > > > Date: Saturday October 29th, 2011[1]
> > > > Time: TBC (probably from 10am-6pm)
> > > > Location: University of Westminster, New Cavendish Street Campus, London, W1W 6UW[2]
> > > > Price: TBC (it'll be in the ballpark of past Manors)
> > > >
> > > > Like previous Manors (and perhaps in the future, all right-thinking conferences) the community will generate and shape the agenda by proposing talks they're willing to give, and providing suggestions on how to shape the talks they'd like to hear. Unlike previous Manors, they'll be doing it on http://vestibule.rubymanor.org/ instead of the mailing list.
> > > >
> > > > Vestibule is where you, the attendees, will develop the talks we'll eventually hear in October. It's a simple tool; all you need is a Twitter login and the beginning of an idea and you're set. Vestibule is also open source[3], and we'll be building on it (perhaps with your help?) over the next few weeks. We'll be in touch in a few days explaining the rationale for something beyond a mailing list for discussing talks this year.
> > > >
> > > > For all peripheral conversation and questions about other parts of the Ruby Manor experience, the mailing list[4] is where it's at, and together with the blog[5] and Twitter accounts[6], it's where you can expect to be kept up to date with what's going on.
> > > >
> > > > Tickets will go on general sale at the start of October. There'll be about 150 of them, so there should be plenty for everyone. That said, we want to make sure that the people who are the most involved are able to come. Therefore if you contribute to the agenda (or general experience) via Vestibule or the mailing list, we'll make sure there's a ticket with your name on it if you want to buy it. Finally, as with previous Manors, if you propose a talk that is selected you'll get a ticket for free (or a refund if you've already bought one).
> > > >
> > > > We'll be in touch with more details as we finalize them. In the meantime, feel free to ask any questions and get collaborating on those talk proposals!
> > > >
> > > > Cheers
> > > >
> > > > Murray, James, Tom
> > > >
> > > > [1]: http://lanyrd.com/2011/ru3y-manor/
> > > > [2]: http://osm.org/go/euu4PIcEx-
> > > > [3]: http://github.com/h-lame/vestibule
> > > > [4]: http://groups.google.com/group/ruby-manor
> > > > [5]: http://blog.rubymanor.org
> > > > [6]: http://twitter.com/rubymanor
> > > >
> > > > _______________________________________________
> > > > Chat mailing list
> > > > Chat at lists.lrug.org (mailto:Chat at lists.lrug.org)
> > > > http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> > >
> > >
> > > _______________________________________________
> > > Chat mailing list
> > > Chat at lists.lrug.org (mailto:Chat at lists.lrug.org)
> > > http://lists.lrug.org/listinfo.cgi/chat-lrug.org
> > >
> >
> > _______________________________________________
> > 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/20110906/6168517c/attachment-0001.htm>
>
> ------------------------------
>
> Message: 2
> Date: Wed, 7 Sep 2011 09:29:06 +0100
> From: Michael Pavling <pavling at gmail.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: [LRUG] Array.slice question
> Message-ID:
> <CA+_7RLwvuzqYVsBh5JdkJXorOK5-CjbJy1D6Z-zG7O90-OBm4A at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> 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
>
>
> ------------------------------
>
> Message: 3
> Date: Wed, 7 Sep 2011 09:33:12 +0100
> From: Neil Middleton <neil.middleton at gmail.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: Re: [LRUG] Array.slice question
> Message-ID: <BF2EEF4FCB8E4CD7A09E9E3B03A68521 at gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> 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-0001.htm>
>
> ------------------------------
>
> Message: 4
> Date: Wed, 7 Sep 2011 09:34:56 +0100
> From: Michael Pavling <pavling at gmail.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: Re: [LRUG] Array.slice question
> Message-ID:
> <CA+_7RLyL+eCbOVxH5Yzp-M5NY-Cqcphkp17fHzXx7tmXwDqXZA at mail.gmail.com>
> Content-Type: text/plain; charset=ISO-8859-1
>
> On 7 September 2011 09:33, Neil Middleton <neil.middleton at gmail.com> wrote:
> > 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.
>
> But if x[3] is nil (because it's out of range), how come starting the
> slice at 3 doesn't count as out of range too?:-/
>
>
> ------------------------------
>
> Message: 5
> Date: Wed, 7 Sep 2011 09:40:02 +0100
> From: Neil Middleton <neil.middleton at gmail.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: Re: [LRUG] Array.slice question
> Message-ID: <08528E5D18534115A92C89134FC69262 at gmail.com>
> Content-Type: text/plain; charset="utf-8"
>
> Ah yes, see what you mean. Good question ;)
>
> Neil
>
> On Wednesday, 7 September 2011 at 09:34, Michael Pavling wrote:
>
> > On 7 September 2011 09:33, Neil Middleton <neil.middleton at gmail.com (mailto:neil.middleton at gmail.com)> wrote:
> > > 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.
> >
> > But if x[3] is nil (because it's out of range), how come starting the
> > slice at 3 doesn't count as out of range too?:-/
> > _______________________________________________
> > 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/47bc435e/attachment-0001.htm>
>
> ------------------------------
>
> Message: 6
> Date: Wed, 7 Sep 2011 09:45:49 +0100
> From: Tom Stuart <tom at experthuman.com>
> To: London Ruby Users Group <chat at lists.lrug.org>
> Subject: Re: [LRUG] Array.slice question
> Message-ID: <AEF128E4-AE4B-4812-8F30-2D954A8551C3 at experthuman.com>
> Content-Type: text/plain; charset=us-ascii
>
> On 7 Sep 2011, at 09:29, Michael Pavling wrote:
> > 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]
> > => []
>
> Not really an explanation, but this is just how rb_ary_subseq works[1]. It's noted as a special case in the Array#slice documentation[2].
>
> Cheers,
> -Tom
>
> [1] https://github.com/ruby/ruby/blob/trunk/array.c#L952
> [2] http://ruby-doc.org/core/classes/Array.html#M000267
>
> ------------------------------
>
> _______________________________________________
> Chat mailing list
> Chat at lists.lrug.org
> http://lists.lrug.org/listinfo.cgi/chat-lrug.org
>
>
> End of Chat Digest, Vol 68, Issue 10
> ************************************
_______________________________________________
Chat mailing list
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/c1f510d2/attachment.html>
More information about the Chat
mailing list