Robert Klemme
4/8/2008 9:11:00 PM
On 08.04.2008 18:56, Kyle Schmitt wrote:
> In fact there _is_ slice :)
> which takes the same args.
> a.slice(1..-2)
> and even a destructive
> a.slice!(1..-2)
> but for some reason my ruby really didn't like it... so never mind the
> destructive part....
> On Tue, Apr 8, 2008 at 11:41 AM, Xavier Noria <fxn@hashref.com> wrote:
>> On Apr 8, 2008, at 18:30 , Mark Dodwell wrote:
>> Hehe.
>>
>> I think the natural idiom is a slice:
>>
>> a[1..-2]
Slice works pretty well - unless the array is small:
irb(main):004:0> a=(1..5).to_a
=> [1, 2, 3, 4, 5]
irb(main):005:0> a.slice! 1...-1
=> [2, 3, 4]
irb(main):006:0> a=[]
=> []
irb(main):007:0> a.slice! 1...-1
=> nil
irb(main):008:0> a
=> [nil]
irb(main):009:0> a=[1]
=> [1]
irb(main):010:0> a.slice! 1...-1
=> []
irb(main):011:0>
irb(main):012:0* a
=> [1]
Kind regards
robert