Ruby Forum Ruby-dev > Array#map$B$,(BEnumerator$B$rJV$5$J$$(B

Posted by unknown (Guest)
on 07.05.2008 21:34
(Received via mailing list)
$B$k$S$-$A$G$9!#(B

Array#map$B$G%V%m%C%/$r$D$1$J$$>l9g$K(BEnumerator$B$rJV$7$^$;$s!#(B

RUBY_VERSION                           # => "1.9.0"
[8,3,9].map                            # => 
#<Enumerable::Enumerator:0xb7aba718>
[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)
Posted by Nobuhiro IMAI (Guest)
on 08.05.2008 01:53
(Received via mailing list)
$B$$$^$$$G$9!#(B

From: rubikitch_at_ruby-lang.org
Date: Thu, 8 May 2008 04:32:52 +0900

> # ~> -:11: undefined method `with_index' for [8, 3, 9]:Array (NoMethodError)
NEWS $B$KCm5-$,$"$j$^$9$M!#(B

(...)
    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]}

$B$G$7$g$&$+!#(B
Posted by unknown (Guest)
on 08.05.2008 04:51
(Received via mailing list)
From: Nobuhiro IMAI <nov@yo.rim.or.jp>
Subject: [ruby-dev:34606] Re: 
Array#map$B$,(BEnumerator$B$rJV$5$J$$(B
Date: Thu, 8 May 2008 08:52:57 +0900

>     Return an enumerator if no block is given.
> 
>     Note that #map and #collect still return an array unlike Ruby 1.9
>     to keep compatibility.

$B$"$j$c$^(B(^^;
map$B$N$?$a$@$1$K(BArray#with_index$B$rDI2C$9$k$N$O$5$9$,$K$d$j$9$.$G$9$+$M!#(B
Posted by Akinori MUSHA (Guest)
on 10.05.2008 15:28
(Received via mailing list)
At Thu, 8 May 2008 08:52:57 +0900,
Nobuhiro IMAI wrote:
> >
>
>     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] }

でもいいです。