Array#slice oddity...

Ask a Question related to Ruby, Design and Development.

  1. #1

    Default Array#slice oddity...

    It appears that if you use slice or slice! with a length argument, it
    returns an array even if the index is out of range:

    irb(main):001:0> a = []
    => []
    irb(main):002:0> b = a.slice(0)
    => nil
    irb(main):003:0> b = a.slice(0,1)
    => []

    Here's the output of ruby -v:

    ruby 1.8.0 (2003-08-04) [i686-linux]

    --
    Matthew Berg <galt@gothpoodle.com>


    Matthew Berg Guest

  2. Similar Questions and Discussions

    1. Date oddity in Acrobat, default page size oddity
      Got two things driving me nuts. One, when I go to Save As a file, the file dats come up in the form like this: Saturday, July -2147483642, 41221,...
    2. Array.slice seems a bit difference from the document
      Hi, I have an array, named as allList And every time I slice a part of the allList to another array, named as partList partList =...
    3. Toolbox oddity
      Greetings All. Does anyone know why the toolbox icons and all the tool options along the top are solid black on some machines and not others, i've...
    4. Slice an array of hashes?
      Is it possible to slice a list of hashes? I read in data into the @data array as follows: while ( $i < $numLev ) { $line = <$fh>; ...
    5. OE oddity
      Greetings, I recently let a friend use my computer for a few minutes to send some e-mail and somehow they managed to mess things up somewhat. ...
  3. #2

    Default Re: Array#slice oddity...

    Matthew Berg wrote:
    > It appears that if you use slice or slice! with a length argument, it
    > returns an array even if the index is out of range:
    >
    > irb(main):001:0> a = []
    > => []
    > irb(main):002:0> b = a.slice(0)
    > => nil
    > irb(main):003:0> b = a.slice(0,1)
    > => []
    Shouldn't a[0] and a[0,1][0] return the same value?


    Joel VanderWerf Guest

  4. #3

    Default Re: Array#slice oddity...

    On Tue, 2003-11-11 at 16:45, Joel VanderWerf wrote:
    > Matthew Berg wrote:
    > > It appears that if you use slice or slice! with a length argument, it
    > > returns an array even if the index is out of range:
    > >
    > > irb(main):001:0> a = []
    > > => []
    > > irb(main):002:0> b = a.slice(0)
    > > => nil
    > > irb(main):003:0> b = a.slice(0,1)
    > > => []
    >
    > Shouldn't a[0] and a[0,1][0] return the same value?
    That they do...

    irb(main):002:0> a[0]
    => nil
    irb(main):003:0> a[0,1][0]
    => nil

    Another thing I noticed is that this behaviour is only exhibited if the
    start index is 0:

    irb(main):004:0> a[0,1]
    => []
    irb(main):005:0> a[1,2]
    => nil

    The same thing happens with ranges:

    irb(main):006:0> a[0..1]
    => []
    irb(main):007:0> a[1..2]
    => nil

    --
    Matthew Berg <galt@gothpoodle.com>


    Matthew Berg Guest

  5. #4

    Default Re: Array#slice oddity...

    Matthew Berg wrote:
    > Another thing I noticed is that this behaviour is only exhibited if the
    > start index is 0:
    >
    > irb(main):004:0> a[0,1]
    > => []
    > irb(main):005:0> a[1,2]
    > => nil
    >
    > The same thing happens with ranges:
    >
    > irb(main):006:0> a[0..1]
    > => []
    > irb(main):007:0> a[1..2]
    > => nil
    I think it depends on how far off the end of the
    array you go.

    x = [1,2,3]
    x[3,3] # []
    x[4,4] # nil

    There's a kind of logic to it, but it requires long and
    hard thought (for me, anyway). Search the archives.

    Hal



    Hal Fulton Guest

  6. #5

    Default Re: Array#slice oddity...

    On Tue, 2003-11-11 at 17:09, Hal Fulton wrote:
    > Matthew Berg wrote:
    > > Another thing I noticed is that this behaviour is only exhibited if the
    > > start index is 0:
    > >
    > > irb(main):004:0> a[0,1]
    > > => []
    > > irb(main):005:0> a[1,2]
    > > => nil
    > >
    > > The same thing happens with ranges:
    > >
    > > irb(main):006:0> a[0..1]
    > > => []
    > > irb(main):007:0> a[1..2]
    > > => nil
    >
    > I think it depends on how far off the end of the
    > array you go.
    >
    > x = [1,2,3]
    > x[3,3] # []
    > x[4,4] # nil
    Just to clarify, "how far off the end" in this case seems to be
    determined solely by the start value, not the length:

    x[3,1000000000] # []
    x[4,1] # nil

    So I guess it gives you an empty array if you request a start index one
    past the end of the array, but only if you didn't specify a length.
    > There's a kind of logic to it, but it requires long and
    > hard thought (for me, anyway). Search the archives.
    I'll take a look to see if I can find anything on it. One way or
    another I can work around it even if it seems a bit counterintuitive.
    :)
    --
    Matthew Berg <galt@gothpoodle.com>


    Matthew Berg Guest

  7. #6

    Default Re: Array#slice oddity...

    Matthew Berg wrote:
    >>There's a kind of logic to it, but it requires long and
    >>hard thought (for me, anyway). Search the archives.
    >
    >
    > I'll take a look to see if I can find anything on it. One way or
    > another I can work around it even if it seems a bit counterintuitive.
    > :)
    It has something to do with imagining a caret or cursor
    in between the elements. When the cursor is outside the
    array but at least adjacent to it, that's one case. When
    it's just totally outside the bounds, that's the other
    case.

    You might try searching the archives for "right out" --
    a reference to the Holy Hand Grenade of Antioch. ;)

    Hal


    Hal Fulton Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139