[Feature #1840] Hash に要素追加して self を返すメソッド

Feature #1840: Hash e$B$KMWAGDI2C$7$Fe(B self e$B$rJV$9%a%=%C%Ie(B
http://redmine.ruby-lang.org/issues/show/1840

e$B5/I<<Te(B: Kimura Yoshihiro
e$B%9%F!<%?%9e(B: Open, e$BM%@hEYe(B: Low

e$BG[Ns$+$ie(B Hash e$B$r:n$k$H$-$J$I$K!"$h$/e(B

a.inject({}) {|h, x| h[x] = ; h }

e$B$_$?$$$K$9$k$N$O$b$O$d%$%G%#%*%`[email protected]$C$F$h$$$H;W$$$^$9$,e(B(1)e$B!"e(B
e$B!Ve(B; he$B!W$NItJ,$,>/!9%
%^%8%J%$$C$]$$$N$,5$$K$J$C$F$$$^$7$?!#e(B

e$B5$$K$J$C$?$D$$$G$K8!:w$7$F$$?$iB>$K$b$=$N$h$&$J0U8+$rL$K$7$?$N$Ge(B(*2)e$B!"e(B
Hash e$B$K!VMWAGDI2C$7$Fe(B self
e$B$rJV$9!W$h$&$J%a%=%C%I$rDI2C$9$k$3$H$re(B
e$BDs0F$7$F$
$^$9!#e(B

e$B$$$/$D$+A*Br;h$r9M$($F$_$^$7$?!'e(B

  1. Hash#store e$B$,e(B self e$B$rJV$9e(B
    e$B;EMM$,[email protected]$NLL$GLdBj$,=P$k$+$b$7$l$^$;$s!#e(B
    e$B$7$+$be(B h.store(x, ) [email protected]$H;zLL$,B?$/$J$k$H$$$&!#e(B
    h.store(*v) e$B$N$h$&$K=q$1$k%1!<%9$J$iC;$+$/$J$k$+$b$7$l$^$;$s!#e(B

  2. Array#insert e$B$K9g$o$;$Fe(B Hash#insert e$B$rDI2C$9$ke(B
    []=, store, insert e$B$H;w$?$h$&$J$N$,J#?t$"$k$N$O!"=i?4<T$K$H$C$Fe(B
    e$BC1$K$o$+$j$K$/[email protected]$1$+$b$7$l$^$;$s!#e(B

  3. Hash#<< e$B$rDI2C$9$ke(B
    ML e$B$r8!:w$7$?$i!"e(B[ruby-list:44686] e$B$G$^$D$b$H$5$s$,!"e(B

    e$B$3$3$+$i$NN`[email protected]$&$He(BHashe$B$K!Ve(B<<e$B!W1i;;;R$rDI2C$7$?$H$7$F$b!"e(B

    a << b

    e$B$Oe(B a.store(*b)
    e$B$H4|BT$5$l$F$7$^$&$N$G$O$J$$$G$7$g$&$+!#>/$Je(B

    e$B$/$H$b;d$O:G=i$=$&;W$$$^$7$?!#e(B

    e$B$H$*$C$7$c$C$F$$$k$N$r8+$D$1$^$7$?!#e(B
    e$B$3$l$r;H$&$H!e(Bh << [k, v] << e$B$7$Fe(B h.assoc(k)
    e$B$7$?7k2L$,e(B Array
    e$B$H$^$C$?$/F1$8$K$J$k!$H$$$&H~$7$5$,$"$j$^$9!#e(B
    e$B$^$?!"%-!<$HCM$NG[Ns$KBP$7$Fe(B a.inject({}, &:<<)
    e$B$G:Q$s$G$7$^$&$N$Oe(B
    e$B$&$l$7$$$+$b$7$l$^$;$s!#e(B

  4. Enumerable#each_with_object e$B$r;H$&e(B
    e$B$=$l$O$=$l$G$"$j$+$b$7$l$^$;$s!#8D?ME*$K$O!e(Beach
    e$B$N$D$$$G$K!De(B e$B$He(B
    e$B$$$&L>A0$re(B object
    e$B$,M_$7$$6ILL$G;H$&$N$O$J$s$H$J$/0cOB46$,$$j$^$9!#e(B
    e$B;w$?L>A0$Ne(B each_with_index e$B$,e(B self
    e$B$rJV$9$+$i$+$b$7$l$^$;$s!#e(B

  5. e$B$=$NB>e(B
    e$B$3$N%$%G%#%%`$r;H$&6ILL$O!"%-!<$N=89g$rJL$N=89g$K%^%C%T%s%0$7$??7e(B
    e$B$7$$e(B Hash e$B$,M_$7$$!"$H$$$&%1!<%[email protected]$H;W$o$l$^$9!#$=$l$re(B
    e$B!V%-!<$N=89g$Ke(B {}e$B$re(B inject
    e$B$9$k!W$HI=8=$9$k$3$H$,!"$=$b$=$bD>46E
    $Ge(B
    e$B$J$$$H$b9M$($i$l$^$9!#e(B

    e$B!Ve(Bzip e$B$7$Fe(B flatten e$B$7$Fe(B *

e$BE83+!W$H$$$&$N$HF1MM$Ke(B

Hash
[email protected]@.MQ$N%/%i%9%a%=%C%I$H$7$F!Nc$($P$3$s$J$N$,$$k$HJXMx$+$be(B
e$B$7$l$^$;$se(B(*3)e$B!#e(B

 def Hash.map(enum)
   if block_given?
     enum.inject({}) {|h, k| h[k] = yield k; h }
   else
     enum.inject({}) {|h, v| h.store(*v); h }
   end
 end

 # ex
 Hash.map(string_keys, &:size)              # e$BJ8;zNse(B => 

e$B%5%$%:e(B
Hash.map(values.each_with_index) # e$BCMe(B =>
e$B%$%s%G%/%9e(B
Hash.map(other_hash.map {|k, v| [v, k] }) # e$BCMe(B => e$B%-!<e(B

e$BB>$NJ}$N0U8+$d%"%$%G%"$bJ9$$$F$_$?$$$G$9!#e(B

(1)
Google Code Search e$B$G!Ve(Blang:ruby
inject({}).+;\s
\w+\s*}e$B!We(B
e$B$H$d$C$?$ie(B 2,000 e$B7o%R%C%H$7$^$7$?!#e(B

(*2)
http://subtech.g.hatena.ne.jp/cho45/20071128/1196239612 e$B$H$+e(B
http://d.hatena.ne.jp/takkan_m/20071212/1197466247 e$B$H$+e(B
http://wota.jp/ac/?date=20081025#p09 e$B$H$+e(B
e$BK?e(Bche$B$H$+e(B

(*3)
e$B2a5n!"K?e(Bche$B$K;w$?$h$&[email protected]$N$O;d$G$9e(B

Kimura Yoshihiro :

Hash#update

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs