At Sun, 18 Feb 2001 12:19:41 +0900,
Guy N. Hurst <gnhurst / hurstlinks.com> wrote:

> > >a = "FooBar"
> > >b = a.split(//)
> > >b[6,2]          #=> []
> > >a[6,2]          #=> nil
> > >
> > >anyone knows why this last one returns nil instead of "" (empty string) ?
> > >
> > >matju
> > 
> > a[6] doesn't exist thus a[6,2] doesn't exist, thus Ruby says "Ain't no such
> > thing"
> 
> Yes, but b[6] doesn't exist, either, so why should b[6,2] exist?

maybe just a typo?

in rb_ary_entry(), offset/beg is checked with 

    if (offset < 0 || RARRAY(ary)->len <= offset) {
	return Qnil;
    }

however, in rb_ary_subseq(), offset/beg is checked with

    if (beg > RARRAY(ary)->len) return Qnil;

so the patch should be like this?

--- array.c	2001/02/14 05:51:57	1.41
+++ array.c	2001/02/18 04:02:03
@@ -400,7 +400,7 @@
 {
     VALUE ary2;
 
-    if (beg > RARRAY(ary)->len) return Qnil;
+    if (beg >= RARRAY(ary)->len) return Qnil;
     if (beg < 0 || len < 0) return Qnil;
 
     if (beg + len > RARRAY(ary)->len) {

now ruby prints

ruby -e "p 'FooBar'.split(//)[6]"      #=> nil
ruby -e "p 'FooBar'.split(//)[6,1]"    #=> nil

instead of 

ruby -e "p 'FooBar'.split(//)[6]"      #=> nil
ruby -e "p 'FooBar'.split(//)[6,1]"    #=> []

--
        yashi