{"id":288,"date":"2016-07-29T15:15:12","date_gmt":"2016-07-29T06:15:12","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/07\/29\/three_reasons_not_to_use_hashie"},"modified":"2022-10-26T10:08:00","modified_gmt":"2022-10-26T01:08:00","slug":"three_reasons_not_to_use_hashie","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2016\/07\/29\/three_reasons_not_to_use_hashie\/","title":{"rendered":"hashie gem\u3092\u4f7f\u308f\u306a\u30443\u3064\u306e\u7406\u7531"},"content":{"rendered":"
\u5f0a\u793e\u3067\u306fSlack\u4e0a\u3067\u3001`pokemon-go`\u677f\u304c\u51fa\u6765\u305f\u308a\u30ab\u30b9\u30bf\u30e0\u30ea\u30a2\u30af\u30b7\u30e7\u30f3 <\/div>\n

<\/p>\n

\u304c\u5927\u6d41\u884c\u3057\u305f\u308a\u306a\u3069\u3057\u3066\u3044\u307e\u3059\u304c\u3001Pokemon GO\u306b\u3044\u307e\u3044\u3061\u30d4\u30f3\u3068\u304d\u3066\u3044\u306a\u3044\u524d\u7530\u3067\u3059\u3002<\/div>\n
\n

hashie<\/a>\u3068\u3044\u3046ruby\u306egem\u304c\u3042\u308a\u307e\u3059\u3002
\n\u7686\u3055\u3093\u306f\u4f7f\u3063\u3066\u3044\u307e\u3059\u3067\u3057\u3087\u3046\u304b\uff1f
\n\u5148\u65e5\u5f0a\u793e\u306e\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30c1\u30fc\u30e0\u3067Ruby\u306egem\u3067\u3042\u308bhashie\u3092\u4f7f\u7528\u3059\u308b\u304b\u3069\u3046\u304b\u3001\u3067\u8b70\u8ad6\u304c\u3042\u308a\u307e\u3057\u305f\u3002
\nomniauth gem \u3067\u4f7f\u7528\u3057\u3066\u3044\u305f\u308a\u306a\u3069\u3001\u30b9\u30bf\u30fc\u3082\u7d50\u69cb\u4ed8\u3044\u3066\u3044\u308bgem\u3067\u3059\u304c\u3001\u73fe\u5728\u5f0a\u793e\u3067\u306f\u57fa\u672c\u7684\u306b\u306f\u4f7f\u7528\u3057\u3066\u3044\u307e\u305b\u3093\u3002
\n\u672c\u65e5\u306f\u305d\u306e\u4e3b\u306a\u7406\u7531\u30923\u3064\u66f8\u304d\u305f\u3044\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n

hashie\u3092\u4f7f\u308f\u306a\u3044\u7406\u7531 1<\/h2>\n

\u4f7f\u7528\u3057\u3066\u3082\u53ef\u8aad\u6027\u306f\u305d\u308c\u307b\u3069\u4e0a\u304c\u3089\u305a\u3001\u4f7f\u7528\u3057\u306a\u3044\u7b87\u6240\u304c\u6df7\u5728\u3059\u308b\u3068\u6df7\u4e71\u306e\u3082\u3068\u306b\u306a\u308b<\/strong><\/p>\n

\u767a\u7aef\u306f\u79c1\u304c\u66f8\u3044\u305f\u3001\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30b3\u30fc\u30c9\u3067\u3057\u305f\u3002<\/p>\n

# params = { maker_cd: 100, grade_cd: 200 }\n\n# \u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\ndef index\n  Car.by_params(Hashie::Mash.new(params))\nend\n\n# \u30e2\u30c7\u30eb\nclass Car < ActiveRecord::Base\n  scope :by_params, lambda { |params|\n    by_maker_cd(params.maker_cd)\n    .by_grade_cd(params.grade_cd)\n  }\nend<\/code><\/pre>\n

\u4ee5\u524d\u3001Hashie::Mash\u3092\u4f7f\u3063\u3066\u5b9f\u88c5\u3057\u305f\u969b\u3001[]<\/code>\u3058\u3083\u306a\u304f\u3066.<\/code>\u306e\u5f62\u3067\u547c\u3079\u308b\u306e\u306f\u3059\u3054\u304f\u4fbf\u5229\u3060\u306a\u3041\u3068\u601d\u3063\u3066\u9699\u3042\u3089\u3070\u826f\u304f\u4f7f\u3063\u3066\u3044\u307e\u3057\u305f\u3002<\/p>\n

\u3057\u304b\u3057\u3001\u4ed6\u306eHashie::Mash\u3092\u4f7f\u308f\u306a\u3044\u4eba\u304c\u3053\u306e\u30b3\u30fc\u30c9\u3092\u898b\u305f\u6642\u306b\u30af\u30ec\u30fc\u30e0\u304c\u51fa\u3066\u3057\u307e\u3044\u307e\u3057\u305f\u3002
\nscope\u306bHashie::Mash\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u6e21\u3055\u306a\u3044\u3068\u3044\u3051\u306a\u3044\u306e\u306f\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u3067\u306f\u5206\u304b\u3089\u306a\u3044\u3057\u3001\u4e00\u90e8Hashie::Mash\u3092\u4f7f\u7528\u3057\u3066\u3001Hashie::Mash\u3092\u4f7f\u7528\u3057\u306a\u3044\u7b87\u6240\u304c\u3042\u308c\u3070\u5168\u4f53\u306e\u7d71\u4e00\u611f\u304c\u3067\u306a\u304f\u306a\u308b\u3057\u3001\u30d0\u30b0\u306e\u6e29\u5e8a\u306b\u306a\u308a\u3084\u3059\u3044\u3002<\/p>\n

\u307e\u305f\u3001\u4e0a\u8a18\u306e\u30b3\u30fc\u30c9\u306f\u3001<\/p>\n

def index\n  Car.by_params(params)\nend\n\nclass Car < ActiveRecord::Base\n  scope :by_params, lambda { |params|\n    by_maker_cd(params[:maker_cd])\n    .by_grade_cd(params[:grade_cd])\n  }\nend<\/code><\/pre>\n

\u3068\u3001\u666e\u901a\u306f\u3053\u306e\u3088\u3046\u306b\u66f8\u304f\u3068\u601d\u3044\u307e\u3059\u304c\u3001Hashie::Mash\u3092\u4f7f\u3063\u305f\u6642\u3068\u4f7f\u308f\u306a\u3044\u6642\u3067\u30b3\u30fc\u30c9\u304c\u898b\u3084\u3059\u304f\u306a\u3063\u305f\u3068\u304b\u3068\u3044\u3048\u3070\u3001\u305d\u308c\u307b\u3069\u591a\u304d\u306a\u9055\u3044\u304c\u3042\u308b\u308f\u3051\u3067\u306f\u3042\u308a\u307e\u305b\u3093\u3002
\n\u3067\u3042\u308c\u3070\u4f7f\u308f\u306a\u3044\u307b\u3046\u304c\u4e07\u4eba\u5411\u3051\u306e\u30b3\u30fc\u30c9\u3067\u3042\u308a\u3001Hashie::Mash\u304c\u4f7f\u308f\u308c\u3066\u3044\u305f\u308a\u4f7f\u308f\u308c\u3066\u3044\u306a\u304b\u3063\u305f\u308a\u3001\u3068\u3044\u3063\u305f\u3053\u3068\u3092\u610f\u8b58\u3057\u306a\u304f\u3066\u3082\u3088\u3044\u30b3\u30fc\u30c9\u306e\u307b\u3046\u304c\u826f\u3044\u3067\u3059\u3088\u306d\u3002<\/p>\n

hashie\u3092\u4f7f\u308f\u306a\u3044\u7406\u7531 2<\/h2>\n

\u6570\u5b57\u304c\u30ad\u30fc\u306e\u5834\u5408\u306f\u30c9\u30c3\u30c8\u3067\u30a2\u30af\u30bb\u30b9\u51fa\u6765\u306a\u3044<\/strong><\/p>\n

[1] pry(main)> m = Hashie::Mash.new(1 => 'one')\n=> {"1"=>"one"}\n[2] pry(main)> m.1\nSyntaxError: unexpected tINTEGER, expecting '('\n[3] pry(main)> m[1]\n=> "one"<\/code><\/pre>\n

Hash\u306f\u6570\u5b57\u3092\u30ad\u30fc\u306b\u51fa\u6765\u308b\u3002
\nHashie::Mash\u3067\u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u751f\u6210\u3057\u3066\u3082\u3001[]<\/code>\u3067\u30a2\u30af\u30bb\u30b9\u3067\u304d\u308b\u304c.<\/code>\u3067\u30a2\u30af\u30bb\u30b9\u51fa\u6765\u306a\u3044\u3002
\n\u3053\u308c\u3067\u306f\u901a\u5e38\u306e\u30cf\u30c3\u30b7\u30e5\u3068\u5909\u308f\u308a\u306f\u7121\u3044\u306e\u3067Hashie::Mash\u3092\u4f7f\u7528\u3059\u308b\u610f\u5473\u306f\u7121\u3044\u3002
\n\u30cf\u30c3\u30b7\u30e5\u5f62\u5f0f\u3067\u6570\u5b57\u304c\u30ad\u30fc\u3068\u3044\u3046\u3053\u3068\u306f\u3042\u307e\u308a\u7121\u3044\u30b1\u30fc\u30b9\u304b\u3082\u3057\u308c\u306a\u3044\u304c\u3001\u3053\u3046\u3044\u3046\u5236\u9650\u304c\u3042\u308b\u3001\u3068\u3044\u3046\u306e\u306f\u898b\u843d\u3068\u3057\u3084\u3059\u3044\u306e\u3067\u3001\u5fc5\u8981\u306e\u7121\u3044\u7b87\u6240\u306b\u307e\u3067Hashie::Mash\u3092\u4f7f\u7528\u3057\u305f\u308a\u3057\u3066\u3044\u308b\u3068\u3001\u6c17\u3065\u304b\u306a\u3044\u3046\u3061\u306b\u30d0\u30b0\u3092\u30b3\u30fc\u30c9\u306b\u57cb\u3081\u8fbc\u3093\u3067\u3057\u307e\u3046\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<\/p>\n

hashie\u3092\u4f7f\u308f\u306a\u3044\u7406\u7531 3<\/h2>\n

\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u578b\u3092\u5f37\u5236\u3059\u308b\u6a5f\u80fd\u304c\u3042\u308b\u3051\u3069..<\/strong><\/p>\n

\u4f8b\u3048\u3070\u4e0b\u8a18\u306e\u3088\u3046\u306a\u30af\u30e9\u30b9\u3092\u4f5c\u3063\u3066\u5404\u30d1\u30e9\u30e1\u30fc\u30bf\u306e\u578b\u3092\u5f37\u5236\u3057\u305f\u3044\u6642\u304c\u3042\u308b\u3002<\/p>\n

class Person < Hashie::Mash\n  include Hashie::Extensions::Coercion\n  include Hashie::Extensions::MergeInitializer\n\n  coerce_key :name, String\n  coerce_key :age, Integer\nend\n\n\n[1] pry(main)> man = Person.new(name: 111, age: '25')\n=> {"name"=>"111", "age"=>25}\n[2] pry(main)> man.name\n=> "111"\n[3] pry(main)> man.age\n=> 25<\/code><\/pre>\n

\u4fbf\u5229\u3067\u3059\u304c\u3001\u305d\u308c\u3063\u3066**Virtus<\/a>**\u3067\u51fa\u6765\u308b\u3088\u306d\u3002<\/p>\n

class Animal\n  include Virtus.model\n\n  attribute :kind, String\n  attribute :age, Integer\nend\n\n[1] pry(main)> dog = Animal.new(kind: 123, age: '3')\n=> #<Animal:0x00563a43c65da8 @age=3, @kind="123">\n[2] pry(main)> dog.kind\n=> "123"\n[3] pry(main)> dog.age\n=> 3<\/code><\/pre>\n

\u3057\u304b\u3082\u3053\u3061\u3089\u306e\u307b\u3046\u304c\u30b7\u30f3\u30d7\u30eb\uff01<\/p>\n


\n

\u3082\u3063\u3068\u6df1\u3044\u6a5f\u80fd\u3092\u4f7f\u3048\u3070Hashie::Mash\u3092\u4f7f\u7528\u3057\u305f\u307b\u3046\u304c\u3044\u3044\u5834\u9762\u304c\u3042\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u304c\u3001\u3068\u308a\u3042\u3048\u305a\u5f0a\u793e\u3067\u306f\u5f53\u5206\u4f7f\u7528\u3059\u308b\u30b7\u30fc\u30f3\u306f\u7121\u3055\u305d\u3046\u3067\u3059\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

\u5f0a\u793e\u3067\u306fSlack\u4e0a\u3067\u3001`pokemon-go`\u677f\u304c\u51fa\u6765\u305f\u308a\u30ab\u30b9\u30bf\u30e0\u30ea\u30a2\u30af\u30b7\u30e7\u30f3 \u304c\u5927\u6d41\u884c\u3057\u305f\u308a\u306a\u3069\u3057\u3066\u3044\u307e\u3059\u304c\u3001Pokemon GO\u306b\u3044\u307e\u3044\u3061\u30d4\u30f3\u3068\u304d\u3066\u3044\u306a\u3044\u524d\u7530\u3067\u3059\u3002 hashie\u3068\u3044\u3046ruby\u306egem\u304c\u3042\u308a\u307e\u3059\u3002 \u7686\u3055\u3093\u306f\u4f7f\u3063\u3066\u3044\u307e\u3059\u3067\u3057\u3087\u3046\u304b\uff1f \u5148\u65e5\u5f0a\u793e\u306e\u30b5\u30fc\u30d0\u30fc\u30b5\u30a4\u30c9\u30c1\u30fc\u30e0\u3067Ruby\u306egem\u3067\u3042\u308bhashie\u3092\u4f7f\u7528\u3059\u308b\u304b\u3069\u3046\u304b\u3001\u3067\u8b70\u8ad6\u304c\u3042\u308a\u307e\u3057\u305f\u3002 omniauth gem \u3067\u4f7f\u7528\u3057\u3066\u3044 […]<\/p>\n","protected":false},"author":1,"featured_media":684,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[42],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/288"}],"collection":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/comments?post=288"}],"version-history":[{"count":1,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/288\/revisions"}],"predecessor-version":[{"id":3364,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/posts\/288\/revisions\/3364"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/"}],"wp:attachment":[{"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/media?parent=288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/categories?post=288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/wp-json\/wp\/v2\/tags?post=288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}