Ruby で hashie (gem) を使ってみる
Ruby
Published: 2019-08-05

やったこと

今回、gem の hashie を使ってみます。

確認環境

$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]

調査

インストール

$ gem install hashie

Coercion

データの型を強制します。

require 'hashie'

class BaseCoercionHash < Hash
  include Hashie::Extensions::Coercion
  include Hashie::Extensions::MergeInitializer
end

class DummyJob < BaseCoercionHash
end

class Job < BaseCoercionHash
  coerce_key :name, String
  coerce_key :year, Integer
end

class User < BaseCoercionHash
  coerce_key :name, String
  coerce_key :age, Integer
  coerce_key :job, Job
end


user = User.new(name: 'hogehoge', age: 20)
p user

user2 = User.new(name: 'hogehoge', age: 'ああああ')
p user2

user3 = User.new(name: 'hogehoge', age: 35, job: Job.new(name: 'free'))
p user3


user4 = User.new(name: 'hogehoge', age: 35, job: {name: 'free2'})
p user4

user5 = User.new(name: 'hogehoge', age: 35, job: DummyJob.new)
p user5

user6 = User.new(name: 'hogehoge', age: 35, job: 99999)
p user6

出力結果

ruby-hashie.rb

$ ruby ruby-hashie.rb
{:name=>"hogehoge", :age=>20}
{:name=>"hogehoge", :age=>0}
{:name=>"hogehoge", :age=>35, :job=>{:name=>"free"}}
{:name=>"hogehoge", :age=>35, :job=>{:name=>"free2"}}
{:name=>"hogehoge", :age=>35, :job=>{}}
Traceback (most recent call last):
	8: from ruby-hashie.rb:39:in `<main>'
	7: from ruby-hashie.rb:39:in `new'
	6: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:20:in `initialize'
	5: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:20:in `each'
	4: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:21:in `block in initialize'
	3: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/coercion.rb:38:in `set_value_with_coercion'
	2: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/coercion.rb:180:in `block in build_coercion'
	1: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/coercion.rb:180:in `new'
/Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:20:in `initialize': undefined method `each' for 99999:Integer (NoMethodError)
	6: from ruby-hashie.rb:39:in `<main>'
	5: from ruby-hashie.rb:39:in `new'
	4: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:20:in `initialize'
	3: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:20:in `each'
	2: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/merge_initializer.rb:21:in `block in initialize'
	1: from /Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/coercion.rb:37:in `set_value_with_coercion'
/Users/abc/.rbenv/versions/2.6.3/lib/ruby/gems/2.6.0/gems/hashie-3.6.0/lib/hashie/extensions/coercion.rb:40:in `rescue in set_value_with_coercion': Cannot coerce property :job from Integer to Job: undefined method `each' for 99999:Integer (Hashie::CoercionError)

DummyJob クラスが Hash クラスを継承していると、通ってしまいました。

気をつけないと嵌るかもしれません。

参考