Bug #8801
closed
Splatted assignment with Enumerator::Lazy
Description
Actual:
a = [1,2,3].lazy
=> #<Enumerator::Lazy: [1, 2, 3]>
b, *c = a
=> #<Enumerator::Lazy: [1, 2, 3]>
b
=> #<Enumerator::Lazy: [1, 2, 3]>
c
=> []
Expected:
a = [1,2,3].lazy
=> #<Enumerator::Lazy: [1, 2, 3]>
b, *c = a
=> #<Enumerator::Lazy: [1, 2, 3]>
b
=> 1
c
=> [2, 3]
Basically, I expect a lazy array to act like a non-lazy one, and that using a destructuring assignment like this is basically the same as calling #take(1) and #drop(1).
Updated by Anonymous almost 12 years ago
- Status changed from Open to Rejected
This is by design. Normal enumerators also work this way:
a = [1,2,3].each
=> #<Enumerator: ...>
b, *c = a
=> #<Enumerator: ...>
b
=> #<Enumerator: ...>
c
=> []
Arrays are the only type of object that will unpack themselves when used with splatted assignment like this.
If you splat the right hand side as well, this will force the Enumerator::Lazy to an array and the assignment will work as you expect it:
a = [1,2,3].lazy
=> #<Enumerator::Lazy: ...>
b, *c = *a
=> [1, 2, 3]
b
=> 1
c
=> [2, 3]
Updated by Anonymous almost 12 years ago
Good to know, thanks!