Array#shuffle! returns nil?

okkez e$B$G$9!#e(B

Array#shuffle! e$B$O$I$&$$$&>l9g$Ke(B nil e$B$rJV$9$N$G$7$g$&$+!)e(B
e$B$=$l$H$be(B RDoc e$B$,4V0c$C$F$$$k$N$G$7$g$&$+!)e(B

e$B%j%U%!%l%s%9%^%K%e%"%k$K%5%s%W%k$r=q$-$?$$$N$G!"e(B
e$B65$($F$$$?$@$1$k$H=u$+$j$^$9!#e(B

$ ruby -v
ruby 1.8.7 (2008-06-09 patchlevel 17) [i686-linux]

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:35034] Array#shuffle! returns nil ?”
on Tue, 10 Jun 2008 14:00:56 +0900, okkez [email protected]
writes:

|Array#shuffle! e$B$O$I$&$$$&>l9g$Ke(B nil e$B$rJV$9$N$G$7$g$&$+!)e(B
|e$B$=$l$H$be(B RDoc e$B$,4V0c$C$F$$$k$N$G$7$g$&$+!)e(B

nile$B$OJV$7$^$;$s$M!#!Ve(B!e$B7O$OJQ99$,$J$+$C$?;~$Ke(Bnile$B$rJV$9!W$H$$e(B
e$B$&86B’$,$"$j$^$9$,!“e(Bshufflee$B$O!VJQ99$,$J$$!W$3$H$,$”$j$($J$$e(B
e$B$N$G>o$Ke(Bselfe$B$rJV$7$^$9!#$3$l$Oe(Breversee$B$K$b8@$($k$3$H$G$9!#e(B

e$B>.GH$G$9!#e(B

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

|Array#shuffle! e$B$O$I$&$$$&>l9g$Ke(B nil e$B$rJV$9$N$G$7$g$&$+!)e(B
|e$B$=$l$H$be(B RDoc e$B$,4V0c$C$F$$$k$N$G$7$g$&$+!)e(B

nile$B$OJV$7$^$;$s$M!#!Ve(B!e$B7O$OJQ99$,$J$+$C$?;~$Ke(Bnile$B$rJV$9!W$H$$e(B
e$B$&86B’$,$"$j$^$9$,!“e(Bshufflee$B$O!VJQ99$,$J$$!W$3$H$,$”$j$($J$$e(B
e$B$N$G>o$Ke(Bselfe$B$rJV$7$^$9!#$3$l$Oe(Breversee$B$K$b8@$($k$3$H$G$9!#e(B

e$B$k$j$^$N$?$a$Ke(B shuffle!
e$B$N2r@b$rF|K\8l$KLu$7$?$N$O;d$G$9$,!$$Ie(B
e$B$&$$$&>r7o$Ge(B nil
e$B$,JV$5$l$k$N$+$h$/J,$+$i$J$$$^$^%(%i!<$r7Q>5e(B
e$B$7$F$7$^$$$^$7$?!#XPXC!*e(B

e$BJY6/$N$?$a$K$J9$-$7$F$$-$?$$$N$G$9$,!$2<$Ne(B array.c e$B$Ne(B
rb_ary_shuffle_bang(VALUE ary)e$B$N%3!<%I$G!$e(Breturn ary;
e$B$7$+$J$$$H$$$&$3$H$+$i!$>e5-$NH=CG$O$G$-$k$N$G$7$g$&$+!#e(B
call-seq: e$B$N$[$&$O4V0c$C$F$$$k$o$1$G$9$,!#e(B

e$B$"$H!$e(Bnil e$B$,JV$k$N$Oe(B return Qnil;
e$B$GH=CG$G$-$k$H$$$&$3$He(B
e$B$G$h$$$N$G$7$g$&$+!#e(B

/*

  • call-seq:
  • array.shuffle!        -> array or nil
    
  • Shuffles elements in self in place.
    */

static VALUE
rb_ary_shuffle_bang(VALUE ary)
{
long i = RARRAY_LEN(ary);

rb_ary_modify(ary);
while (i) {

long j = rb_genrand_real()*i;
VALUE tmp = RARRAY_PTR(ary)[–i];
RARRAY_PTR(ary)[i] = RARRAY_PTR(ary)[j];
RARRAY_PTR(ary)[j] = tmp;
}
return ary;
}

e$B$^$D$b$He(B e$B$f$-$R$m$G$9e(B

In message “Re: [ruby-dev:35038] Re: Array#shuffle! returns nil ?”
on Tue, 10 Jun 2008 16:58:25 +0900, “Hideo Konami”
[email protected] writes:

|e$B$k$j$^$N$?$a$Ke(B shuffle! e$B$N2r@b$rF|K\8l$KLu$7$?$N$O;d$G$9$,!$$Ie(B
|e$B$&$$$&>r7o$Ge(B nil e$B$,JV$5$l$k$N$+$h$/J,$+$i$J$$$^$^%(%i!<$r7Q>5e(B
|e$B$7$F$7$^$$$^$7$?!#XPXC!*e(B

e$B$$$($$$(!"85$,4V0c$C$F$k$N$$$1$J$$$o$1$G!#e(B

|e$BJY6/$N$?$a$K$J9$-$7$F$$-$?$$$N$G$9$,!$2<$Ne(B array.c e$B$Ne(B
|rb_ary_shuffle_bang(VALUE ary)e$B$N%3!<%I$G!$e(Breturn ary;
|e$B$7$+$J$$$H$$$&$3$H$+$i!$>e5-$NH=CG$O$G$-$k$N$G$7$g$&$+!#e(B
|call-seq: e$B$N$[$&$O4V0c$C$F$$$k$o$1$G$9$,!#e(B

Ce$B$G$9$+$i!"e(Breturne$B$7$J$$$H$3$m$+$iCM$,JQ$($k$3$H$,$J$$$N$OL@e(B
e$B$i$+$G!"e(B

  • return arye$B$7$+$J$$e(B
  • ary(e$BBh0l0z?te(B)e$B$Oe(Bselfe$B$@$+$ie(Bnile$B$G$O$"$j$($J$$e(B
  • e$B4X?tCf$Ge(Barye$B$r=q$-49$($F$$$J$$e(B

e$B$3$H$+$ie(Bnile$B$,JV$i$J$$$3$H$rH=CG$G$-$^$9!#e(B

|e$B$"$H!$e(Bnil e$B$,JV$k$N$Oe(B return Qnil; e$B$GH=CG$G$-$k$H$$$&$3$He(B
|e$B$G$h$$$N$G$7$g$&$+!#e(B

return Qnil;e$B$,9T$o$l$F$$$l$P3N<B$G$9$,!“$=$NB>e(Breturne$B$G;XDj$5e(B
e$B$l$?$b$N$,e(Bnile$B$G$J$$$+$H$&$+3NG’$9$kI,MW$O$”$j$^$9!#e(B

                            e$B$^$D$b$He(B e$B$f$-$R$me(B /:|)

okkez e$B$G$9!#e(B

2008/06/10 15:20 Yukihiro M. [email protected]:

e$B$N$G>o$Ke(Bselfe$B$rJV$7$^$9!#$3$l$Oe(Breversee$B$K$b8@$($k$3$H$G$9!#e(B

e$B@bL@$"$j$,$H$&$4$6$$$^$9!#e(B

e$B$b$&$A$g$C$H$A$c$s$He(BCe$B$N%3!<%IFI$a$k$h$&$K$J$j$?$$!#!#!#e(B