Hash$B$+$i(BStruct$B$r:n$k(B

e$B$k$S$-$A$G$9!#e(B

Hashe$B$+$ie(BStructe$B$r:n$l$?$i$$$$$J$H;W$$$^$7$?!#e(B

def Struct(hash)
Struct.new(*hash.keys.map(&:to_sym)).new(*hash.values)
end
s = Struct(“a”=>1, :b=>2) # => #
s.a # => 1
s.b # => 2
s.a+=23
s # => #

e$BM}M3$O!"e(B

  • OpenStructe$B$Oe(BHashe$B$+$i:n@.$G$-$k!#e(B
  • OpenStructe$B$Oe(B method_missing
    e$B$r;H$C$F$$$k$?$a!"e(Btypoe$B$K<e$$!#e(B
  • accessore$B7A<0$G%-!<$K%"%/%;%9$7$?$,$k?M$r$h$/8+3]$1$k!#e(B
  • Structe$B$J$N$Ge(Btypoe$B$,H/8+$7$d$9$$!#e(B

Structe$B<+BNe(BHashe$B$He(BArraye$B$NAPJ}$N@-<A$r;}$A9g$o$;$F$$$k!#e(B

e$B!Ve(BKernel.#Structe$B!W!"!Ve(BStruct.[]e$B!W!"!Ve(BStruct.newe$B!W$N0z?t$Ke(BHashe$B$r;XDj$7$Fe(B
e$B:n$l$k$H$$$$$H;W$$$^$9!#e(B

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

In message “Re: [ruby-dev:33387] Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Sat, 26 Jan 2008 03:41:32 +0900, [email protected] writes:

|Hashe$B$+$ie(BStructe$B$r:n$l$?$i$$$$$J$H;W$$$^$7$?!#e(B

e$B$I$&$$$&e(BAPIe$B$,NI$$$H;W$$$^$9$+!#Nc$G$Oe(BStructe$B$H$$$&L>A0$N4X?te(B
e$B$rF3F~$7$F$$$^$9$,!"$=$l$,:GA1$@$H;W$$$^$9$+!)e(B

|e$B!Ve(BKernel.#Structe$B!W!"!Ve(BStruct.[]e$B!W!"!Ve(BStruct.newe$B!W$N0z?t$Ke(BHashe$B$r;XDj$7$Fe(B
|e$B:n$l$k$H$$$$$H;W$$$^$9!#e(B

e$B!Ve(BKernel.Structe$B!W!"!Ve(BStruct.[]e$B!W$O?7@_$K$J$j$^$9$,!"K\Ev$K?7e(B
e$B$7$$%a%=%C%I$,I,MW$G$9$+!)e(B

e$B!Ve(BStruct.newe$B!W$r3HD%$9$k>l9g!"%a%s%P!<$,$R$H$D$7$+$J$1$l$P0Ue(B
e$BL#$,[#Kf$K$J$k$N$G$9$,!"$I$A$i$rA*Br$9$Y$-$@$H;W$$$^$9$+!)e(B
e$B$^$?!"$=$l$K$h$j%W%m%0%i%`$,$R$H$a$G$o$+$i$J$/$J$kB&LL$,A}$(e(B
e$B$?$j$7$^$;$s$+!)e(B

|e$BM}M3$O!"e(B
|* OpenStructe$B$Oe(BHashe$B$+$i:n@.$G$-$k!#e(B

OpenStructe$B$Oe(BHashe$B$G$9$+$i$M!#8GDj%a%s%P!<$Ne(BStructe$B$N>l9g!"%a%se(B
e$B%P!<$H$7$FB8:_$7$J$$e(Bkeye$B$,;XDj$5$l$?$i$I$&$J$k$+!"$"$k$$$O5Ue(B
e$B$K;XDj$5$l$J$$%a%s%P!<$,$"$C$?>l9g$I$&$9$k$+$rL@3N$K$7$J$1$le(B
e$B$P$J$j$^$;$s$M!#e(B

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

From: Yukihiro M. [email protected]
Subject: [ruby-dev:33455] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Mon, 28 Jan 2008 17:59:52 +0900

e$B$k$S$-$A$G$9!#e(B

|Hashe$B$+$ie(BStructe$B$r:n$l$?$i$$$$$J$H;W$$$^$7$?!#e(B

e$B$I$&$$$&e(BAPIe$B$,NI$$$H;W$$$^$9$+!#Nc$G$Oe(BStructe$B$H$$$&L>A0$N4X?te(B
e$B$rF3F~$7$F$$$^$9$,!"$=$l$,:GA1$@$H;W$$$^$9$+!)e(B

e$B$3$NA0$O@*$$$G!Ve(BKernel.Structe$B!W$H=q$-$^$7$?$,!"e(B
Struct.newe$B$r3HD%$9$k$+!"e(BStruct.for_hashe$B$N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).new(1)e$B!W$N$h$&$J0UL#$G$7$g$&$+!)e(B
e$B$=$l$J$i$P!Ve(BStruct.new(:a=>1, :b=>nil)e$B!W$G$$$$$H;W$$$^$9!#e(B

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

In message “Re: [ruby-dev:33505] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Tue, 29 Jan 2008 22:28:14 +0900, [email protected] writes:

|> e$B!Ve(BStruct.newe$B!W$r3HD%$9$k>l9g!"%a%s%P!<$,$R$H$D$7$+$J$1$l$P0Ue(B
|> e$BL#$,[#Kf$K$J$k$N$G$9$,!"$I$A$i$rA*Br$9$Y$-$@$H;W$$$^$9$+!)e(B
|> e$B$^$?!"$=$l$K$h$j%W%m%0%i%`$,$R$H$a$G$o$+$i$J$/$J$kB&LL$,A}$(e(B
|> e$B$?$j$7$^$;$s$+!)e(B
|
|e$B$9$_$^$;$s!"$I$&$$$&$3$H$G$7$g$&$+!)e(B
|e$B$?$H$($P!Ve(BStruct.new(:a => 1)e$B!W$O!Ve(BStruct.new(:a).new(1)e$B!W$N0UL#$7$+;W$$IU$-$^$;$s!#e(B
|Hashe$B$+$ie(BStructe$B$r:n$k>l9g$O!"!Ve(BStruct.new(hash).members == hash.keyse$B!W$rA[Dj$7$^$9!#e(B

e$B$"!<!"$A$g$C$H4*0c$$$7$F$^$7$?!#e(B

Struct.new(:a, :b).new(:a => 1, :b => 2)

e$B$N$h$&$Ke(BStruct#newe$B$r3HD%$9$k$3$H$r9M$($F$^$7$?!#e(B

e$B!Ve(BStruct.new(:a =>
1)e$B!W$G!Ve(BStruct.new(:a).new(1)e$B!W$N0UL#$J$se(B
e$B$G$9$M!#%W%m%0%i%`$K$A$c$s$H$=$&=q$$$F$"$k$N$K!#e(B

e$B$^!"$=$l$O;d$N4V0c$$$J$s$GCV$$$H$$$F!"$=$&$9$k$H!"%$%s%9%?%se(B
e$B%9$,$R$H$D$7$+$J$$e(BStructe$B$rKh2s:n$k$3$H$K$J$j$^$9$M!#$=$l$C$Fe(B
e$B$I$l$@$1$&$l$7$$$s$G$9$+$M!#30$+$i8+$k8B$j!“C1$J$ke(BHashe$B$HBg:9e(B
e$B$J$$$h$&$J!#$b$A$m$sB?>/%a%b%j8zN($O0c$$$^$9$,!”$=$l0U30$KFCe(B
e$B$K$&$l$7$$$3$H$O$J$5$=$&$J>e$K!"@8@.$9$k$N$Ke(BHashe$B$R$H$D!"%$%se(B
e$B%9%?%s%9$4$H$KL5L>%/%i%9$R$H$D:n$C$A$c$&$N$G$=$3$^$G%a%b%j8ze(B
e$BN($b$h$/$O$J$$$h$&$J!#e(B

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

From: Yukihiro M. [email protected]
Subject: [ruby-dev:33507] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Tue, 29 Jan 2008 23:34:17 +0900

e$B!Ve(BStruct.new(:a => 1)e$B!W$G!Ve(BStruct.new(:a).new(1)e$B!W$N0UL#$J$se(B
e$B$G$9$M!#%W%m%0%i%`$K$A$c$s$H$=$&=q$$$F$"$k$N$K!#e(B

e$B$^!“$=$l$O;d$N4V0c$$$J$s$GCV$$$H$$$F!”$=$&$9$k$H!“%$%s%9%?%se(B
e$B%9$,$R$H$D$7$+$J$$e(BStructe$B$rKh2s:n$k$3$H$K$J$j$^$9$M!#$=$l$C$Fe(B
e$B$I$l$@$1$&$l$7$$$s$G$9$+$M!#30$+$i8+$k8B$j!“C1$J$ke(BHashe$B$HBg:9e(B
e$B$J$$$h$&$J!#$b$A$m$sB?>/%a%b%j8zN($O0c$$$^$9$,!”$=$l0U30$KFCe(B
e$B$K$&$l$7$$$3$H$O$J$5$=$&$J>e$K!”@8@.$9$k$N$Ke(BHashe$B$R$H$D!"%$%se(B
e$B%9%?%s%9$4$H$KL5L>%/%i%9$R$H$D:n$C$A$c$&$N$G$=$3$^$G%a%b%j8ze(B
e$BN($b$h$/$O$J$$$h$&$J!#e(B

e$B$d$C$Q$je(B st.a e$B$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 2, :c => 3)’
#

e$B5DO@$N$?$?$-Bf$K$J$l$P9,$$$G$9!#e(B

Index: struct.c

— struct.c (revision 15331)
+++ struct.c (working copy)
@@ -281,6 +281,13 @@
return make_struct(nm, ary, rb_cStruct);
}

+static int
+keyvalue_i(VALUE key, VALUE value, void *ptr) {

  • VALUE arg = (VALUE) ptr;
  • rb_ary_push(arg[0], key);
  • rb_ary_push(arg[1], value);
    +}

/*

  • call-seq:
  • Struct.new( [aString] [, aSym]+> )    => StructClass
    

@@ -318,12 +325,19 @@
static VALUE
rb_struct_s_def(int argc, VALUE *argv, VALUE klass)
{

  • VALUE name, rest;
  • VALUE name, rest, values = Qfalse;
    long i;
    VALUE st;
    ID id;
  • rb_scan_args(argc, argv, “1*”, &name, &rest);
  • if (rb_scan_args(argc, argv, “1*”, &name, &rest) == 1 &&
    TYPE(name) == T_HASH) {

  • VALUE arg[2];

  • rest = arg[0] = rb_ary_new();

  • values = arg[1] = rb_ary_new();

  • rb_hash_foreach(name, keyvalue_i, (VALUE)&arg);

  • name = Qnil;

  • }

  • for (i=0; i<RARRAY_LEN(rest); i++) {
    id = rb_to_id(RARRAY_PTR(rest)[i]);
    RARRAY_PTR(rest)[i] = ID2SYM(id);
    @@ -337,6 +351,10 @@
    rb_mod_module_eval(0, 0, st);
    }

  • if (values) {

  • return rb_struct_alloc(st, values);

  • }

  • return st;
    }

From: Yukihiro M. [email protected]
Subject: [ruby-dev:33512] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Wed, 30 Jan 2008 01:11:09 +0900

e$B$U$&$`!#e(B[ruby-dev:33509]e$B$N%3!<%I$b8+$F$$^$7$?$,!“$d$O$j0z?te(B
e$B%Q%?!<%s$K$h$C$FLa$jCM$,6KC<$K0c$&$N$O$”$^$jK>$^$7$/$J$$$h$&e(B
e$B$J5$$,$7$^$9$N$G!"e(BStruct.newe$B$N3HD%$O;
$a$F$*$-$^$;$s$+!#e(B

e$B$&!A$`!"$=$&$G$9$M!#e(B

Struct.for_hashe$B$K$D$$$F$O!“@5D>$”$^$jL>A0$,9%$_$8$c$J$$$s$Ge(B
e$B$9$h$M$(!#e(B

Struct.newe$B$N3HD%$r$d$a$k$H$7$F$b!"JL$JJ}K!$Ge(BHashe$B$+$i:n$j$?$$$G$9!#e(B
e$B%@%a%b%H$Ge(BStruct.from_hashe$B$H$+e(BHash#to_structe$B$H$+8@$C$F$_$k!#e(B

Foo = Struct.new(:f)
Foo.new(:f => 42)

e$B$H$7$?;~$K!"e(Bfe$B$H$$$&%a%s%P$Oe(B{:f=>42}e$B$H$$$&%O%C%7%e$K=i4|2=$5e(B
e$B$l$k$+!"e(B42e$B$H$$$&@0?t$K=i4|2=$5$l$k$+[#Kf$K$J$k$H$$$&7gE@$,$"e(B

e$B$“$”!"$=$&$+!#LUE@$@$C$?!#e(B

e$B$j$^$9!#$^$"!“e(B1e$BMWAG$N9=B$BN$OLGB?$K$J$$$G$7$g$&$+$i$”$^$jLde(B
e$BBj$K$J$i$J$$$+$b$7$l$^$;$s$,!#e(B

1e$BMWAG$N9=B$BN$G$7$+$be(BHashe$B$N>l9g$J$s$F$“$^$j$J$$$N$G;EMMJQ99$b$”$j$@$H;W$$$^$9!#e(B

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

In message “Re: [ruby-dev:33508] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Tue, 29 Jan 2008 23:46:40 +0900, [email protected] writes:

|e$B$d$C$Q$je(B st.a e$B$N$h$&$K%"%/%;%5$G%"%/%;%9$G$-$k$3$H$G$9!#e(B
|e$B%"%/%;%5$NJ}$,%*%V%8%’%/%H;X8~$7$F$$$k46$8$@$7!"e(B
|e$B%a%s%PL>$NBG$A4V0c$$$r$7$h$&$b$N$J$i%(%i!<$rEj$2$F$/$l$k$N$G;H$&J}$H$7$F$Oe(B
|e$B$H$F$b4r$7$$$G$9!#e(B

e$B$U$&$`!#e(B[ruby-dev:33509]e$B$N%3!<%I$b8+$F$$^$7$?$,!"$d$O$j0z?te(B
e$B%Q%?!<%s$K$h$C$FLa$jCM$,6KC<$K0c$&$N$O$"$^$jK>$^$7$/$J$$$h$&e(B
e$B$J5$$,$7$^$9$N$G!"e(BStruct.newe$B$N3HD%$O;
$a$F$*$-$^$;$s$+!#e(B

Struct.for_hashe$B$K$D$$$F$O!"@5D>$"$^$jL>A0$,9%$_$8$c$J$$$s$Ge(B
e$B$9$h$M$(!#e(B

|> Struct.new(:a, :b).new(:a => 1, :b => 2)
|
|e$B$3$C$A$b$$$$$G$9$M!#e(B

e$B0lJ}!"$3$C$A$G$9$,!"$3$l$O$3$l$G0UL#$,$"$k$H$O;W$$$^$9$7!"e(B
Struct.newe$B$N3HD%$h$j$O9%$_$J$s$G$9$,!"e(B[ruby-dev:33455]e$B$G$b=qe(B
e$B$$$?$h$&$Ke(B

Foo = Struct.new(:f)
Foo.new(:f => 42)

e$B$H$7$?;~$K!"e(Bfe$B$H$$$&%a%s%P$Oe(B{:f=>42}e$B$H$$$&%O%C%7%e$K=i4|2=$5e(B
e$B$l$k$+!"e(B42e$B$H$$$&@0?t$K=i4|2=$5$l$k$+[#Kf$K$J$k$H$$$&7gE@$,$“e(B
e$B$j$^$9!#$^$”!“e(B1e$BMWAG$N9=B$BN$OLGB?$K$J$$$G$7$g$&$+$i$”$^$jLde(B
e$BBj$K$J$i$J$$$+$b$7$l$^$;$s$,!#e(B

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

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

In message “Re: [ruby-dev:33513] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Wed, 30 Jan 2008 01:22:56 +0900, [email protected] writes:

|> Struct.for_hashe$B$K$D$$$F$O!"@5D>$"$^$jL>A0$,9%$$8$c$J$$$s$Ge(B
|> e$B$9$h$M$(!#e(B
|
|Struct.newe$B$N3HD%$r$d$a$k$H$7$F$b!"JL$JJ}K!$Ge(BHashe$B$+$i:n$j$?$$$G$9!#e(B
|e$B%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$3$A$i$OHf3SE*<u$1F~$l$d$9$$$N$G$9e(B
e$B$,!#LGB?$K$J$$$H$O8@$(!"$G$-$J$$e(B(e$B2sHr:v$,@8@.8e$KGK2uE*$K99?7e(B
e$B$9$k$7$+$J$$e(B)e$B$H$$$&$N$O5$$K$J$k$H$$$($P5$$K$J$k$N$G$9$,!#e(B

Structe$B$N%/%i%9%a%=%C%I$NJ}$NL>A0$,7h$^$l$P!"$=$l$K$"$o$;$Fe(B
newe$B0J30$N%a%=%C%IL>$K$7$F$7$^$($P$$$$$N$G$O$J$$$G$7$g$&$+!#e(B

e$B2>$Ke(BStruct.from_hashe$B$J$ie(BStruct#from_hashe$B$N$h$&$K!#e(B

From: Yukihiro M. [email protected]
Subject: [ruby-dev:33542] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Thu, 31 Jan 2008 02:28:43 +0900

e$B$k$S$-$A$G$9!#e(B

|> Struct.for_hashe$B$K$D$$$F$O!“@5D>$”$^$jL>A0$,9%$$8$c$J$$$s$Ge(B
|> e$B$9$h$M$(!#e(B
|
|Struct.newe$B$N3HD%$r$d$a$k$H$7$F$b!"JL$JJ}K!$Ge(BHashe$B$+$i:n$j$?$$$G$9!#e(B
|e$B%@%a%b%H$Ge(BStruct.from_hashe$B$H$+e(BHash#to_structe$B$H$+8@$C$F$
$k!#e(B

e$B$^$:!"$R$H$D$7$+%$%s%9%?%s%9$N$J$$e(BStructe$B$r$$$-$J$j:n$k%9%?%$e(B
e$B%k$r?d>)$9$Y$-$+$I$&$+$H$$$&$N$,Bh0l$NgeFq$G$9!#e(B

e$B$3$NDs0F$NH/C<$O!“e(BHashe$B$N%-!<=89g$r8GDj$G$-$J$$$+$H$$$&$3$H$G$9!#e(B
Hashe$B$r9=B$BNE*$K;H$&>l9g!”%-!<$NBG$A4V0c$$$rNc30$G8!=P$G$-$J$$$N$,:GBg$NLdBj$G$9!#e(B
e$B8=>u$G9M$($i$l$k$N$,e(BStructe$B$rMxMQ$9$k$3$H$G$9!#e(B
e$B$J$N$GKM$O$=$s$J$Ke(BStructe$B$K$3$@$o$C$F$$$k$o$1$G$O$"$j$^$;$s!#e(B

e$B$"$k$$$O!"e(BHashe$B$N%-!<$r8GDj$9$k!JDI2CIT2DG=!“B8:_$7$J$$%-!<$K%”%/%;%9$7$?$iNc30!Ke(B
e$B%a%=%C%I$r:n@.$G$-$J$$$b$N$G$7$g$&$+!)e(B
Hash#fix_keys, Hash#freeze_keys, Hash#fix, Hash#lock, Hash#pin,
Hash#fixatee$B!De(B

e$B$^!“$=$l$O;d$N4V0c$$$J$s$GCV$$$H$$$F!”$=$&$9$k$H!“%$%s%9%?%se(B
e$B%9$,$R$H$D$7$+$J$$e(BStructe$B$rKh2s:n$k$3$H$K$J$j$^$9$M!#$=$l$C$Fe(B
e$B$I$l$@$1$&$l$7$$$s$G$9$+$M!#30$+$i8+$k8B$j!“C1$J$ke(BHashe$B$HBg:9e(B
e$B$J$$$h$&$J!#$b$A$m$sB?>/%a%b%j8zN($O0c$$$^$9$,!”$=$l0U30$KFCe(B
e$B$K$&$l$7$$$3$H$O$J$5$=$&$J>e$K!”@8@.$9$k$N$Ke(BHashe$B$R$H$D!"%$%se(B
e$B%9%?%s%9$4$H$KL5L>%/%i%9$R$H$D:n$C$A$c$&$N$G$=$3$^$G%a%b%j8ze(B
e$BN($b$h$/$O$J$$$h$&$J!#e(B

Hashe$B"*e(BStructe$BJQ49$r$9$k%1!<%9$G$OB?$/$N>l9g$R$H$D$7$+%$%s%9%?%s%9$r:n$i$J$$$H;W$$$^$9!#e(B
e$B$3$Ne(BAPIe$B$,$J$/$F$b3F<+$,F1$8$h$&$J$3$H$r$d$k$H;W$$$^$9!#e(B

hash = {:a=>1, :b=>2}
Struct.new(*hash.keys).new(*hash.values) # => #

e$B$G!“2>$K?d>)$9$Y$-$@$H$7$F!”<!$NgeFq$,E,@Z$JL>A0$G$9$M!#e(B

Kernel#BigDecimale$B$de(BKernel#URIe$B$KJo$C$Fe(BKernel#Structe$B$G$I$&$G$7$g$&$+!)e(B
e$BJQ494o$H$$$&0UL#9g$$$G$9!#e(B
e$BEvA3e(BStructe$B$r;XDj$7$?$i<+J,<+?H$rJV$9$h$&$K$7$^$9!#e(B

e$B@.@%$G$9!#e(B

[email protected] wrote:

e$B$3$NDs0F$NH/C<$O!“e(BHashe$B$N%-!<=89g$r8GDj$G$-$J$$$+$H$$$&$3$H$G$9!#e(B
Hashe$B$r9=B$BNE*$K;H$&>l9g!”%-!<$NBG$A4V0c$$$rNc30$G8!=P$G$-$J$$$N$,:GBg$NLdBj$G$9!#e(B
e$B8=>u$G9M$($i$l$k$N$,e(BStructe$B$rMxMQ$9$k$3$H$G$9!#e(B
e$B$J$N$GKM$O$=$s$J$Ke(BStructe$B$K$3$@$o$C$F$$$k$o$1$G$O$"$j$^$;$s!#e(B

Hash#[] e$B$G$J$/e(B Hash#fetch e$B$r;H$($PNc30$,$G$^$9$h!#e(B

From: “NARUSE, Yui” [email protected]
Subject: [ruby-dev:33736] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Wed, 13 Feb 2008 13:23:31 +0900

Hash#[] e$B$G$J$/e(B Hash#fetch e$B$r;H$($PNc30$,$G$^$9$h!#e(B

Hash#fetche$B$O5-=R$,D9$/$J$k$N$G$“$^$j9%$-$G$O$J$$$G$9!#e(B
e$B$=$l$Ke(BHash#fetche$B$KBP1~$9$k99?7%a%=%C%I$O$”$j$^$;$s$h$M!#e(B
e$B$@$+$i$3$=e(BHash#fix(e$B2>e(B)e$B$rDs0F$7$F$$$^$9!#e(B

e$B%-!<=89g$,8GDj$5$l$?e(BHashe$B$Oe(BStructe$B$N$h$&$K%a%=%C%I$G%-!<$K%"%/%;%9$G$-$?$i$H$h$/;W$$$^$9!#e(B
Hash#fix(e$B2>e(B)e$B$GFC0[%a%=%C%I$rDj5A$7$F!De(B

e$B@.@%$G$9!#e(B

[email protected] wrote:

From: “NARUSE, Yui” [email protected]
Subject: [ruby-dev:33736] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Wed, 13 Feb 2008 13:23:31 +0900

Hash#[] e$B$G$J$/e(B Hash#fetch e$B$r;H$($PNc30$,$G$^$9$h!#e(B

Hash#fetche$B$O5-=R$,D9$/$J$k$N$G$“$^$j9%$-$G$O$J$$$G$9!#e(B
e$B$=$l$Ke(BHash#fetche$B$KBP1~$9$k99?7%a%=%C%I$O$”$j$^$;$s$h$M!#e(B

e$B$"!<!“e(BHash#store e$B$@$H%@%a$J$s$G$9$M!”$?$7$+$K!#e(B

e$B$@$+$i$3$=e(BHash#fix(e$B2>e(B)e$B$rDs0F$7$F$$$^$9!#e(B

e$B%-!<=89g$,8GDj$5$l$?e(BHashe$B$Oe(BStructe$B$N$h$&$K%a%=%C%I$G%-!<$K%"%/%;%9$G$-$?$i$H$h$/;W$$$^$9!#e(B
Hash#fix(e$B2>e(B)e$B$GFC0[%a%=%C%I$rDj5A$7$F!De(B

e$B$=$3$^$G9T$/$H!“$=$l$C$F!Ve(BHashe$B!W$J$N$+$J$!$H$$$&5$$,$7$^$9$+$i!”$=$3$GJLe(B
e$B$N%/%i%9$KJQ49!<“*e(BStructe$B$K!”$H$$$&$o$1$G$9$+!"$J$k$[$I!#e(B

e$B1J0f!wCNG=!%6e9)Bg$G$9!%e(B

From: [email protected]
Subject: [ruby-dev:33734] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Wed, 13 Feb 2008 13:07:52 +0900
Message-ID: [email protected]

e$B$3$NDs0F$NH/C<$O!“e(BHashe$B$N%-!<=89g$r8GDj$G$-$J$$$+$H$$$&$3$H$G$9!#e(B
Hashe$B$r9=B$BNE*$K;H$&>l9g!”%-!<$NBG$A4V0c$$$rNc30$G8!=P$G$-$J$$$N$,:GBg$NLdBj$G$9!#e(B
e$B8=>u$G9M$($i$l$k$N$,e(BStructe$B$rMxMQ$9$k$3$H$G$9!#e(B
e$B$J$N$GKM$O$=$s$J$Ke(BStructe$B$K$3$@$o$C$F$$$k$o$1$G$O$"$j$^$;$s!#e(B

e$B$"$k$$$O!"e(BHashe$B$N%-!<$r8GDj$9$k!JDI2CIT2DG=!“B8:_$7$J$$%-!<$K%”%/%;%9$7$?$iNc30!Ke(B
e$B%a%=%C%I$r:n@.$G$-$J$$$b$N$G$7$g$&$+!)e(B
Hash#fix_keys, Hash#freeze_keys, Hash#fix, Hash#lock, Hash#pin, Hash#fixatee$B!De(B

e$B$=$&$7$?$b$N$,9-$/I,MW$H$5$l$F$$$k$N$+$I$&$+$,$o$+$i$J$$$N$G$9$,!$e(B
e$B2<5-$N$h$&$Ke(B (e$B0lIt$@$1<BAue(B)
e$B<+J,$G:n$k$N$G$O%@%a$J$N$G$7$g$&$+!)e(B

e$BCY$$$7LLE]$@$+$i!)e(B


class FixedHash < Hash
def initialize(hash)
hash.each{|key, value| _set(key.to_s, value)}
end

alias :_set :[]=
private :_set

def
raise IndexError, ‘key not found’ unless self.key?(kstr = key.to_s)
super(kstr)
end

def []=(key, val)
raise IndexError, ‘key not found’ unless self.key?(kstr = key.to_s)
super(kstr, val)
end

def method_missing(key, *args)
kstr = key.to_s

if args.empty?
  # reader
  self[kstr]
else
  # writer
  if (kstr = key.to_s)[-1] == ?=
    kstr.chop!
    self[kstr] = args[0]
  else
    self[kstr] = args[0]
    self
  end
end

end
end

if $0 == FILE
p h = FixedHash.new(:a=>1, :b=>2)
p h.a
p h.b
p h.a += 23
p h
p h.a = 3
p h
p h.a(4).b(5)
p h

begin
h.c
rescue
p $!
end

begin
h.c = 1
rescue
p $!
end
end

e$B$J$+$@$G$9!#e(B

At Wed, 13 Feb 2008 13:07:52 +0900,
[email protected] wrote in [ruby-dev:33734]:

e$B$"$k$$$O!"e(BHashe$B$N%-!<$r8GDj$9$k!JDI2CIT2DG=!“B8:_$7$J$$%-!<$K%”%/%;%9$7$?$iNc30!Ke(B
e$B%a%=%C%I$r:n@.$G$-$J$$$b$N$G$7$g$&$+!)e(B
Hash#fix_keys, Hash#freeze_keys, Hash#fix, Hash#lock, Hash#pin, Hash#fixatee$B!De(B

e$B$?$a$7$K:n$C$F$$^$7$?!#e(Bfix!,unfix!,fixed?e$B$rDI2C!"e(B[],[]=,delete
e$B$GB8:
$7$J$$%-!<$K%"%/%;%9$7$?$ie(BKeyErrore$B$rH/@8$7$^$9!#e(B
initialize,shift,clear,delete_ife$B$J$I$NC1FH$G%-!<$r;XDj$9$k$N$G$Je(B
e$B$$$b$N$ONc30$K$O$J$j$^$;$s!#$3$l$@$He(Bfixe$B$H$$$&$N$H$O$A$g$C$H%$%a!<e(B
e$B%8$,0c$&$h$&$J5$$,$7$^$9$,!#e(B

Index: hash.c

— hash.c (revision 15455)
+++ hash.c (working copy)
@@ -25,4 +25,5 @@ static VALUE rb_hash_s_try_convert(VALUE
#define HASH_DELETED FL_USER1
#define HASH_PROC_DEFAULT FL_USER2
+#define HASH_FIXED FL_USER3

VALUE
@@ -426,4 +427,25 @@ rb_hash_rehash(VALUE hash)
}

+VALUE
+rb_hash_fix(VALUE hash)
+{

  • FL_SET(hash, HASH_FIXED);
  • return hash;
    +}

+VALUE
+rb_hash_unfix(VALUE hash)
+{

  • FL_UNSET(hash, HASH_FIXED);
  • return hash;
    +}

+VALUE
+rb_hash_fixed_p(VALUE hash)
+{

  • if (FL_TEST(hash, HASH_FIXED)) return Qtrue;
  • return Qfalse;
    +}

/*

  • call-seq:
    @@ -446,4 +468,7 @@ rb_hash_aref(VALUE hash, VALUE key)

    if (!RHASH(hash)->ntbl || !st_lookup(RHASH(hash)->ntbl, key, &val))
    {

  • if (FL_TEST(hash, HASH_FIXED)) {
  •  rb_raise(rb_eKeyError, "key not found");
    
  • }
    return rb_funcall(hash, id_default, 1, key);
    }
    @@ -683,12 +708,17 @@ VALUE
    rb_hash_delete(VALUE hash, VALUE key)
    {
  • VALUE val;
  • VALUE val = Qundef;
  • rb_hash_modify(hash);
  • val = rb_hash_delete_key(hash, key);
  • rb_hash_modify_check(hash);
  • if (RHASH(hash)->ntbl) {
  • val = rb_hash_delete_key(hash, key);
  • }
    if (val != Qundef) return val;
    if (rb_block_given_p()) {
    return rb_yield(key);
    }
  • else if (FL_TEST(hash, HASH_FIXED)) {
  • rb_raise(rb_eKeyError, “key not found”);
  • }
    return Qnil;
    }
    @@ -736,8 +766,10 @@ rb_hash_shift(VALUE hash)
    struct shift_var var;
  • rb_hash_modify(hash);
  • rb_hash_modify_check(hash);
    var.key = Qundef;
  • rb_hash_foreach(hash, RHASH(hash)->iter_lev > 0 ? shift_i_safe :
    shift_i,
  •    (st_data_t)&var);
    
  • if (RHASH(hash)->ntbl) {

  • rb_hash_foreach(hash, RHASH(hash)->iter_lev > 0 ? shift_i_safe :
    shift_i,

  •  (st_data_t)&var);
    
  • }

    if (var.key != Qundef) {
    @@ -929,10 +961,14 @@ VALUE
    rb_hash_aset(VALUE hash, VALUE key, VALUE val)
    {

  • rb_hash_modify(hash);
  • if (TYPE(key) != T_STRING || st_lookup(RHASH(hash)->ntbl, key, 0))
    {
  • st_insert(RHASH(hash)->ntbl, key, val);
  • rb_hash_modify_check(hash);
  • if (TYPE(key) != T_STRING ||
  • (RHASH(hash)->ntbl && st_lookup(RHASH(hash)->ntbl, key, 0))) {
  • st_insert(rb_hash_tbl(hash), key, val);
    }
    else {
  • st_add_direct(RHASH(hash)->ntbl, rb_str_new4(key), val);
  • if (FL_TEST(hash, HASH_FIXED)) {

  •  rb_raise(rb_eKeyError, "can't insert into fixed hash");
    
  • }

  • st_add_direct(rb_hash_tbl(hash), rb_str_new4(key), val);
    }
    return val;
    @@ -2613,4 +2649,8 @@ Init_Hash(void)
    rb_define_method(rb_cHash,“compare_by_identity?”,
    rb_hash_compare_by_id_p, 0);

  • rb_define_method(rb_cHash,“fix!”, rb_hash_fix, 0);

  • rb_define_method(rb_cHash,“unfix!”, rb_hash_unfix, 0);

  • rb_define_method(rb_cHash,“fixed?”, rb_hash_fixed_p, 0);

#ifndef MACOS /* environment variables nothing on MacOS. */
origenviron = environ;

From: Hidetoshi NAGAI [email protected]
Subject: [ruby-dev:33753] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B
Date: Wed, 13 Feb 2008 16:01:04 +0900

e$B$"$k$$$O!"e(BHashe$B$N%-!<$r8GDj$9$k!JDI2CIT2DG=!“B8:_$7$J$$%-!<$K%”%/%;%9$7$?$iNc30!Ke(B
e$B%a%=%C%I$r:n@.$G$-$J$$$b$N$G$7$g$&$+!)e(B
Hash#fix_keys, Hash#freeze_keys, Hash#fix, Hash#lock, Hash#pin, Hash#fixatee$B!De(B

e$B$=$&$7$?$b$N$,9-$/I,MW$H$5$l$F$$$k$N$+$I$&$+$,$o$+$i$J$$$N$G$9$,!$e(B
e$B2<5-$N$h$&$Ke(B (e$B0lIt$@$1<BAue(B) e$B<+J,$G:n$k$N$G$O%@%a$J$N$G$7$g$&$+!)e(B

e$B<+J,$G:n$k$/$i$$$J$i$Pe(BStructe$B$G==J,$+$H!#e(B
e$B$d$O$j!V%-!<$,8GDj$5$l$?%O%C%7%e!W$Oe(BStructe$B$,E,G$$J$N$G$7$g$&!#e(B

e$B$=$&$$$($Pe(BStruct#each_paire$B$,$"$k$N$Ke(BStruct#to_hashe$B$O$J$$$N$G$9$M!#e(B
e$B0EL[$N7?JQ49$@$+$i$d$j$9$.!)e(B

e$BKM$NCf$G$Oe(BStructe$B$O!V%-!<$,8GDj$5$l$?%O%C%7%e!W$H!VG[Ns!W$NAPJ}$N@-<A$r;}$D%/%i%9$G$9!#e(B
Struct#memberse$B$,$“$k$N$Ke(BStruct#member?e$B$,$J$$$N$b0U30$G$7$?!#e(B
e$B$5$i$Ke(BStruct#keyse$B$He(BStruct#key?e$B$,$=$l$>$le(BStruct#keyse$B$He(BStruct#memberse$B$NJLL>$G$”$k$He(B
e$B@09g@-$,<h$l$F$$$k$H;W$$$^$9!#e(B

e$B$D$$$G$Ke(BPStore#keys, PStore#key?e$B$NJLL>$b4uK>!#e(B

s = Struct.new(:a,:b).new(1,2) # => #
s.to_a # => [1, 2]
s.to_hash rescue $! # => #<NoMethodError: undefined method
`to_hash’ for #>
s.members # => [:a, :b]

Enumerable#member?e$B$,8F$P$l$k$+$ie(Bmemberse$B$H$NBP>N@-$,<h$l$F$$$J$$e(B
s.member? 1 # => true
s.member? :a # => false
s.key? :a rescue $! # => #<NoMethodError: undefined method `key?’ for
#>

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

In message “Re: [ruby-dev:33734] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Wed, 13 Feb 2008 13:07:52 +0900, [email protected] writes:

|e$B$"$k$$$O!"e(BHashe$B$N%-!<$r8GDj$9$k!JDI2CIT2DG=!“B8:_$7$J$$%-!<$K%”%/%;%9$7$?$iNc30!Ke(B
|e$B%a%=%C%I$r:n@.$G$-$J$$$b$N$G$7$g$&$+!)e(B
|Hash#fix_keys, Hash#freeze_keys, Hash#fix, Hash#lock, Hash#pin, Hash#fixatee$B!De(B

e$B$=$l$O$A$g$C$HL5M}6Z$G$7$g$&!#e(B

Struct.xxxx(hash)

e$B$G%O%C%7%e%-!<$r%a%s%P$K;}$De(BStructe$B$rDj5A$7!“BP1~$9$k%P%j%e!<e(B
e$B$G=i4|2=$7$?%$%s%9%?%s%9$r:n$k$H$$$&$N$O%”%j$@$H;W$$$^$9!#$^e(B
e$B$?!"e(B

Struct.new(:foo, :bar, :baz).xxxx2(hash)

e$B$G4{B8$Ne(BStructe$B$r%O%C%7%e$G=i4|2=$9$k$N$b%"%j$G$7$g$&!#$G$b!"e(B
e$B$=$l$>$le(Bnewe$B$H$$$&L>A0$O;_$a$?J}$,$h$$$N$G$O$J$$$G$7$g$&$+!#e(B

|Kernel#BigDecimale$B$de(BKernel#URIe$B$KJo$C$Fe(BKernel#Structe$B$G$I$&$G$7$g$&$+!)e(B
|e$BJQ494o$H$$$&0UL#9g$$$G$9!#e(B
|e$BEvA3e(BStructe$B$r;XDj$7$?$i<+J,<+?H$rJV$9$h$&$K$7$^$9!#e(B

e$B$&!<$s!"e(BStructe$B$KJQ49$7$F$$$k$o$1$G$O$J$$$G$9$+$i$M$(!#e(B

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

In message “Re: [ruby-dev:33756] Re: Hashe$B$+$ie(BStructe$B$r:n$ke(B”
on Wed, 13 Feb 2008 19:18:00 +0900, [email protected] writes:

|e$BKM$NCf$G$Oe(BStructe$B$O!V%-!<$,8GDj$5$l$?%O%C%7%e!W$H!VG[Ns!W$NAPJ}$N@-<A$r;}$D%/%i%9$G$9!#e(B
|Struct#memberse$B$,$"$k$N$Ke(BStruct#member?e$B$,$J$$$N$b0U30$G$7$?!#e(B
|e$B$5$i$Ke(BStruct#keyse$B$He(BStruct#key?e$B$,$=$l$>$le(BStruct#keyse$B$He(BStruct#memberse$B$NJLL>$G$"$k$He(B
|e$B@09g@-$,<h$l$F$$$k$H;W$$$^$9!#e(B

e$B!V%-!<$,8GDj$5$l$?%O%C%7%e!W$N@-<A$O8=;~E@$G$O$"$s$^$j$J$/$F!"e(B
e$B$=$l$r6/2=$9$Y$-$+$I$&$+$O$^$@3N?.$,;}$F$F$$$^$;$s!#e(B