From: mame@... Date: 2015-12-13T08:11:39+00:00 Subject: [ruby-core:72094] [Ruby trunk - Bug #11811] Chaining lazy enumerators causes duplicate ouput Issue #11811 has been updated by Yusuke Endoh. Not only reject's bug. Lazy enumerator ignores &:method generally. ~~~~ $ ./miniruby -e ' s = [1,2,3].lazy.map(&:nonexist) p :foo s.each {|x| p x; 100 + x } ' :foo 1 101 2 102 3 103 ~~~~ Correct behavior: ~~~ $ ruby -ve ' s = [1,2,3].lazy.map(&:nonexist) p :foo s.each {|x| p x; 100 + x } ' ruby 2.2.3p173 (2015-08-18 revision 51636) [x86_64-linux] :foo -e:5:in `each': undefined method `nonexist' for 1:Fixnum (NoMethodError) ~~~ Explicit `.to_proc` works correctly. ~~~ $ ./miniruby -e ' s = [1,2,3].lazy.map(&:nonexist.to_proc) p :foo s.each {|x| p x; 100 + x } ' :foo -e:5:in `each': undefined method `nonexist' for 1:Fixnum (NoMethodError) ~~~ So I guess this is caused by nobu's optimization. -- Yusuke Endoh ---------------------------------------- Bug #11811: Chaining lazy enumerators causes duplicate ouput https://bugs.ruby-lang.org/issues/11811#change-55510 * Author: Chris Beer * Status: Open * Priority: Normal * Assignee: Nobuyoshi Nakada * ruby -v: ruby 2.3.0preview2 (2015-12-11 trunk 53028) [x86_64-darwin15] * Backport: 2.0.0: UNKNOWN, 2.1: UNKNOWN, 2.2: UNKNOWN ---------------------------------------- In Ruby 2.3.0-preview2, I'm seeing a change in behavior using lazy enumerators with select/reject and the & operator: irb(main):037:0> %w(1 2 3).lazy.reject(&:empty?).each { |x| puts x } 1 1 2 2 3 3 Note that the output is doubled. However, if I don't use the & shorthand, the output is as expected: irb(main):038:0> %w(1 2 3).lazy.reject { |x| x.empty? }.each { |x| puts x } 1 2 3 => nil And in Ruby 2.2.3, both variants produced the same result: irb(main):001:0> %w(1 2 3).lazy.reject(&:empty?).each { |x| puts x } 1 2 3 => nil -- https://bugs.ruby-lang.org/