Ruby Forum Ruby-dev > enum_for(:each_with_index)$B$NAj0c(B

Posted by unknown (Guest)
on 11.05.2008 17:53
(Received via mailing list)
$B$k$S$-$A$G$9!#(B

Ruby 1.8.7$B$H(BRuby 1.9$B$N(B each_with_index 
$B$N(BEnumerator$B$N5sF0$,0[$J$j$^$9!#(B

require 'enumerator'
RUBY_VERSION                    # => "1.8.6"
%w[foo bar].enum_for(:each_with_index).select {|x,i| i>=1 } # => 
[["bar", 1]]

require 'enumerator'
RUBY_VERSION                    # => "1.8.7"
%w[foo bar].enum_for(:each_with_index).select {|x,i| i>=1 } # => 
[["bar", 1]]

require 'enumerator'
RUBY_VERSION                    # => "1.9.0"
%w[foo bar].enum_for(:each_with_index).select {|x,i| i>=1 } # => ["bar"]

RUBY_VERSION                    # => "1.8.7"
%w[foo bar].each_with_index.select {|x,i| i>=1 } # => [["bar", 1]]

RUBY_VERSION                    # => "1.9.0"
%w[foo bar].each_with_index.select {|x,i| i>=1 } # => ["bar"]
Posted by Akinori MUSHA (Guest)
on 12.05.2008 13:26
(Received via mailing list)
At Mon, 12 May 2008 00:52:52 +0900,
rubikitch@ruby-lang.org wrote:
> Ruby 1.8.7とRuby 1.9の each_with_index のEnumeratorの挙動が異なります。
> RUBY_VERSION                    # => "1.8.7"
> %w[foo bar].each_with_index.select {|x,i| i>=1 } # => [["bar", 1]]
>
> RUBY_VERSION                    # => "1.9.0"
> %w[foo bar].each_with_index.select {|x,i| i>=1 } # => ["bar"]

 [ruby-dev:32708]→[ruby-dev:32745]という流れがあるようなので
1.9を修正しました。to_aを挟むと挙動が変わるというのは気持ち悪い
こともありますしね。