From: Akinori MUSHA Date: 2008-05-10T22:27:21+09:00 Subject: [ruby-dev:34635] Re: Array#mapがEnumeratorを返さない --pgp-sign-Multipart_Sat_May_10_22:27:22_2008-1 Content-Type: text/plain; charset=ISO-2022-JP At Thu, 8 May 2008 08:52:57 +0900, Nobuhiro IMAI wrote: > いまいです。 > > From: rubikitch_at_ruby-lang.org > Date: Thu, 8 May 2008 04:32:52 +0900 > > > Array#mapでブロックをつけない場合にEnumeratorを返しません。 > > > > RUBY_VERSION # => "1.9.0" > > [8,3,9].map # => # > > [8,3,9].map.with_index{|x,i| [x*x, i]} # => [[64, 0], [9, 1], [81, 2]] > > > > RUBY_VERSION # => "1.8.7" > > [8,3,9].map # => [8, 3, 9] > > [8,3,9].map.with_index{|x,i| [x*x, i]} # => > > > > # ~> -:11: undefined method `with_index' for [8, 3, 9]:Array (NoMethodError) > > NEWS に注記がありますね。 > > (...) > Return an enumerator if no block is given. > > Note that #map and #collect still return an array unlike Ruby 1.9 > to keep compatibility. > (...) > > [8,3,9].enum_for(:map).with_index{|x,i| [x*x, i]} > > でしょうか。 [8,3,9].each_with_index.map { |x,i| [x*x, i] } でもいいです。 -- Akinori MUSHA / http://akinori.org/ --pgp-sign-Multipart_Sat_May_10_22:27:22_2008-1 Content-Type: application/pgp-signature Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.9 (FreeBSD) iEYEABECAAYFAkgloroACgkQkgvvx5/Z4e6oSwCfchIwP2e2pENzb6d4rYHnUjwv H6IAnRg8RyqBiauIx6Bebaev4go3UQJp =6pdO -----END PGP SIGNATURE----- --pgp-sign-Multipart_Sat_May_10_22:27:22_2008-1--