Project

General

Profile

Actions

Bug #8801

closed

Splatted assignment with Enumerator::Lazy

Added by Anonymous almost 12 years ago. Updated almost 12 years ago.

Status:
Rejected
Assignee:
-
Target version:
-
ruby -v:
ruby 2.1.0dev (2013-08-18) [x86_64-darwin11.4.2]
[ruby-core:56713]

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!

Actions

Also available in: Atom PDF

Like0
Like0Like0