Hashie::Mashおかしい
(解決した、下に書いた)
これ実行すると
#!/usr/bin/env ruby require 'rubygems' require 'hashie' arr = Array.new 3.times do |i| arr.push Hashie::Mash.new(:count => i, :text => 'test'*i) end arr.each do |i| p i puts "count : #{i.count}" puts "text : #{i.text}" end
countだけ同じ値がでるんだけど、なんで・・・?
#<Hashie::Mash count=0 text=""> count : 2 text : #<Hashie::Mash count=1 text="test"> count : 2 text : test #<Hashie::Mash count=2 text="testtest"> count : 2 text : testtest
Hash#countで保持しているkeyの数が返ってくるらしい。
hashieはmethod_missingで実装されているので、missingじゃないの呼び出してた。
このへん全部使えない
1.8.7 :002 > h.methods => ["find", "[]=", "inspect", "convert_value", "merge", "tap", "reject!", "clone", "take", "store", "__send__", "object_id", "public_methods", "reject", "instance_variable_defined?", "minmax", "hashie_inspect", "freeze", "equal?", "indexes", "member?", "each", "values", "delete", "sort", "extend", "partition", "each_cons", "send", "any?", "each_with_index", "hashie_stringify_keys!", "default", "methods", "values_at", "initializing_reader", "detect", "hashie_stringify_keys", "hash", "take_while", "default=", "default_proc", "dup", "replace", "instance_variables", "to_enum", "collect", "clear", "stringify_keys", "min_by", "invert", "sort_by", "eql?", "indices", "regular_reader", "size", "has_key?", "group_by", "enum_cons", "instance_eval", "id", "one?", "enum_with_index", "singleton_methods", "find_index", "deep_merge!", "taint", "empty?", "shift", "drop", "each_value", "instance_variable_get", "frozen?", "enum_for", "map", "shallow_merge", "display", "instance_of?", "max_by", "method", "grep", "to_a", "regular_writer", "update", "first", "to_json", "has_value?", "instance_exec", "type", "fetch", "none?", "reverse_each", "stringify_keys!", "rehash", "protected_methods", "find_all", "delete_if", "regular_dup", "method_missing", "==", "min", "to_mash", "===", "drop_while", "each_key", "instance_variable_set", "shallow_update", "each_slice", "key?", "inject", "kind_of?", "minmax_by", "respond_to?", "keys", "count", "to_s", "convert_key", "value?", "class", "to_hash", "index", "zip", "private_methods", "=~", "tainted?", "__id__", "select", "deep_merge", "length", "max", "hash_inspect", "untaint", "nil?", "each_pair", "entries", "deep_update", "cycle", "merge!", "reduce", "enum_slice", "include?", "is_a?", "[]", "all?"]
idとかclassとかcountとかindexとかtypeとかmapとかmaxとかlength使えなかったとはワロタ
こうすると値を取り出せる
1.8.7 :007 > h.count = 30 => 30 1.8.7 :008 > h['count'] => 30