I use a similar function to removeAt.
Just with 2 key differences:
1) Takes start (i) and end (j) indexes. Both are actually optional,
defaulting to length and taking no effect.
2) Returns the altered array with a child array containing the last
group of deleted items.

(Note: simply using [].rem().clone() will remove the child array)

If removeAt is the Ruby port, then this probably won't keep with that
aspect of Prototype.
But, I prefer it since it doesn't break chaining.

Thoughts?


/* Array#del([start [, end]]) => Array (with: deleted => Array) */
Object.extend(Array.prototype, {
  del : function (i, j) {
    i = (Object.isNumber(i) && i >= 0) ? i.floor() : this.length;
    j = (Object.isNumber(j) && j >= i) ? j.floor() : i;
    this.deleted = this.splice(i, ((j - i) + 1));
    return this;
  }
});


[1, 3, 5, 7, 9].del(1).del(2) //=> [1, 5, 9] (with: deleted => [7])

[1, 3, 5, 7, 9].del(3)         //=> [1, 3, 5, 9]
[1, 3, 5, 7, 9].del(5)         //=> [1, 3, 5, 7, 9]
[1, 3, 5, 7, 9].del(3, 3)      //=> [1, 3, 5, 9]
[1, 3, 5, 7, 9].del(3, 4)      //=> [1, 3, 5]

[1, 3, 5].del(0)                 //=> [3, 5]
[1, 3, 5].del(0).deleted       //=> [1]
[1, 3, 5].del(0, 1).deleted    //=> [1, 3]


- Jon L.

On Mar 4, 12:46 pm, Samuel Lebeau <[EMAIL PROTECTED]> wrote:
> I often wanted to be able to simply remove some objects from an array
> without creating another array (using Array#without for instance).
> Array#splice seems to be the only way to change an array in place, and
> using it is not really easy nor readable.
>
> I submitted a patch (http://dev.rubyonrails.org/ticket/11042) which
> defines two methods I find useful:
>   - Array#removeAt(index), which removes an object given its index
>   - Array#removeIf(iterator[, context]), which removes objects for
> which iterator returns a truthy value
>
> These two methods are directly inspired from they Ruby equivalent
> Array#delete_at and Array#delete_if, with "delete" renamed "remove" as
> "delete" is a Javascript keyword, and in anticipation of a future
> Array#remove(object).
> Ruby's Array#delete_if returns instance (this) but this implementation
> returns an array of removed objects, which I think is less suprising
> (please give your opinionon this point).
>
> There are also corresponding unit tests which passes on all supported
> platforms (adapted from Ruby trunk ones), and inline documentation in
> PDoc format.
>
> I hope this will be helpful for everyone !
>
> Regards,
> Samuel Lebeau
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Prototype: Core" group.
To post to this group, send email to [email protected]
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