Forum: Ruby-dev Arrary.newにbreakを含むブロック を渡した時の挙動について

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Hirotsugu A. (Guest)
on 2009-06-07 23:00
(Received via mailing list)
浅里と申します。

題目に沿ってArrayを生成しようとしてbreakするとbreakに
渡された値が無条件に返ってきます。これは仕様で
しょうか。

実際、RubySpecではそのように捉えているようです。
(119行目から126行目まで→http://github.com/rubyspec/rubyspec/blob/c1a7e0ffb...
re/array/new_spec.rb)

しかしながら、これですと

$ ruby -ve 'a=Array.new(3) {|i| break 3 if i==2; i*i}; puts
a.class.inspect'
ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
Fixnum
$ ruby19 -ve 'a=Array.new(3) {|i| break 3 if i==2; i*i}; puts
a.class.inspect'
ruby 1.9.2dev (2009-06-03 trunk 23623) [i386-darwin9.7.0]
Fixnum

という具合になって、よろしくないと思います。

(何がこのブロックでやりたいのかよく解んないです
けど。)
Yukihiro M. (Guest)
on 2009-06-08 02:05
(Received via mailing list)
$B$^$D$b$H(B $B$f$-$R$m$G$9(B

In message "Re: [ruby-dev:38576]
Arrary.new$B$K(Bbreak$B$r4^$`%V%m%C%/$rEO$7$?;~$N5sF0$K$D$$$F(B"
    on Mon, 8 Jun 2009 03:59:04 +0900, Hirotsugu A.
<removed_email_address@domain.invalid> writes:

|$BBjL\$K1h$C$F(BArray$B$r@8@.$7$h$&$H$7$F(Bbreak$B$9$k$H(Bbreak$B$K(B
|$BEO$5$l$?CM$,L5>r7o$KJV$C$F$-$^$9!#$3$l$O;EMM$G(B
|$B$7$g$&$+!#(B

$B%V%m%C%/$G(Bbreak$B$9$k$H$=$NCM$,JV$k$N$O;EMM$G$9!#(B

|$B$7$+$7$J$,$i!"$3$l$G$9$H(B
|
|$ ruby -ve 'a=Array.new(3) {|i| break 3 if i==2; i*i}; puts a.class.inspect'
|ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
|Fixnum
|$ ruby19 -ve 'a=Array.new(3) {|i| break 3 if i==2; i*i}; puts a.class.inspect'
|ruby 1.9.2dev (2009-06-03 trunk 23623) [i386-darwin9.7.0]
|Fixnum
|
|$B$H$$$&6q9g$K$J$C$F!"$h$m$7$/$J$$$H;W$$$^$9!#(B
|
|$B!J2?$,$3$N%V%m%C%/$G$d$j$?$$$N$+$h$/2r$s$J$$$G$9(B
|$B$1$I!#!K(B

$B$J$K$,$d$j$?$$$+$o$+$i$J$$$N$G!"$J$K$,$h$m$7$/$J$$$N$+$o$+$i(B
$B$J$$$G$9!#$J$K$+!V$h$m$7$/$J$$!W$N$G$"$l$P!"$b$&$A$g$C$H6qBN(B
$BE*$K$*4j$$$7$^$9!#(B
Hirotsugu A. (Guest)
on 2009-06-08 02:19
(Received via mailing list)
$B@uN$$G$9!#(B

On Jun 7, 2009, at 5:03 PM, Yukihiro M. wrote:

> |$B$7$g$&$+!#(B
>
> $B%V%m%C%/$G(Bbreak$B$9$k$H$=$NCM$,JV$k$N$O;EMM$G$9!#(B

$BN;2r$7$^$7$?!#(B

> |Fixnum
> |
> |$B$H$$$&6q9g$K$J$C$F!"$h$m$7$/$J$$$H;W$$$^$9!#(B
> |
> |$B!J2?$,$3$N%V%m%C%/$G$d$j$?$$$N$+$h$/2r$s$J$$$G$9(B
> |$B$1$I!#!K(B
>
> $B$J$K$,$d$j$?$$$+$o$+$i$J$$$N$G!"$J$K$,$h$m$7$/$J$$$N$+$o$+$i(B
> $B$J$$$G$9!#$J$K$+!V$h$m$7$/$J$$!W$N$G$"$l$P!"$b$&$A$g$C$H6qBN(B
> $BE*$K$*4j$$$7$^$9!#(B
>

Array.new$B$J$N$KJV$C$F$/$k$N$,2?$+M=4|$G$-$J$$$N$O!V$h$m$7$/$J(B
$B$$!W$N$G$O!"(B
$B$H9M$($?Lu$G$9!#(B
This topic is locked and can not be replied to.