環境
* MacBook Air MC506J/A
* ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-darwin10]
$ cat ./hashsub.rb
#!/usr/bin/env ruby
class MyHash < Hash
def set_all(data)
self.each_key{|key|
self[key] = data
}
end
end
hash1 = Hash.new
hash1["Hash"] = "hash1"
hash1["a"] = "A"
hash1["b"] = "B"
hash1["c"] = "C"
p hash1
hash2 = MyHash.new
hash2["Hash"] = "hash2"
hash2["a"] = "A"
hash2["b"] = "B"
hash2["c"] = "C"
p hash2
hash3 = MyHash.new
hash3["Hash"] = "hash3"
hash3["a"] = "A"
hash3["b"] = "B"
hash3["c"] = "C"
hash3.set_all "XYZ"
p hash3
んで、実行。
$ ruby ./hashsub.rb
{"a"=>"A", "b"=>"B", "c"=>"C", "Hash"=>"hash1"}
{"a"=>"A", "b"=>"B", "c"=>"C", "Hash"=>"hash2"}
{"a"=>"XYZ", "b"=>"XYZ", "c"=>"XYZ", "Hash"=>"XYZ"}
tags: Ruby zurazure
Posted by NI-Lab. (@nilab)