---
title: "สร้าง hashtable ระดับกลางให้อัตโนมัติใน Ruby"
description: "hashtable ระดับกลางทั้งหมดจะถูกสร้างขึ้นให้เองโดยอัตโนมัติ"
canonical: https://oozou.com/til/automagical-intermediate-level-hashtables-in-ruby-62
locale: th
date: 2020-10-13
topic: ruby
---

# สร้าง hashtable ระดับกลางให้อัตโนมัติใน Ruby

> hashtable ระดับกลางทั้งหมดจะถูกสร้างขึ้นให้เองโดยอัตโนมัติ

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

hashtable ระดับกลางทั้งหมดจะถูกสร้างขึ้นให้เองโดยอัตโนมัติ

```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/th/contact

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