koozdra wrote:
> Thanks for the quick reply.
>
> I ran some tests in ruby:
>
> array = [1,2,3,4]
> p array.insert(-3, 'one', 'two')
> array = [1,2,3,4]
> p array.insert(-1, 'one', 'two')
> array = [1,2,3,4]
> p array.insert(0, 'one', 'two')
> array = [1,2,3,4]
> p array.insert(1, 'one', 'two')
> array = [1,2,3,4]
> p array.insert(10, 'one', 'two')
>
> output:
> [1, 2, "one", "two", 3, 4]
> [1, 2, 3, 4, "one", "two"]
> ["one", "two", 1, 2, 3, 4]
> [1, "one", "two", 2, 3, 4]
> [1, 2, 3, 4, nil, nil, nil, nil, nil, nil, "one", "two"]
>
> when the index is negative and of greater length than the array, ruby
> throws an index out of bounds exception.
>   
Based on that info, below is a revised method.  On FF2 it seems to behave the 
same as your Ruby examples.  I made an index of -5 in your example work the 
same as 0.

- Ken


Array.prototype.insert = function() {
  var args = $A(arguments), index = args.shift();
  index += index < 0 ? this.length : 0;
  if (index > this.length || index < -1) 
    throw new Error('Index out of bounds.');
  args = [this[index], 0].concat(args);
  Array.prototype.splice.apply(this, args);
}



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to prototype-core@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/prototype-core?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to