---
title: "Ruby で中間階層のハッシュテーブルを自動生成する"
description: "すべての中間階層のハッシュテーブルが自動的に作成されます"
canonical: https://oozou.com/til/automagical-intermediate-level-hashtables-in-ruby-62
locale: ja
date: 2020-10-13
topic: ruby
---

# Ruby で中間階層のハッシュテーブルを自動生成する

> すべての中間階層のハッシュテーブルが自動的に作成されます

Published: 2020-10-13 · Topic: [ruby](https://oozou.com/ja/til-topics/ruby) ([markdown](https://oozou.com/ja/til-topics/ruby.md))

すべての中間階層のハッシュテーブルが自動的に作成されます

```ruby
def autovivifying_hash
   Hash.new {|ht,k| ht[k] = autovivifying_hash}
end
```

### 使い方

```ruby
response_builder = autovivifying_hash
response_builder['payment']['status'] = 'OK'
response_builder # => {"payment"=>{"status"=>"OK"}
```

---

Contact OOZOU: hello@oozou.com · +66 2544 0000 · https://oozou.com/ja/contact

More for AI agents: https://oozou.com/llms.txt · every page has a markdown twin at its URL plus ".md"
