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