久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

在 Ruby 中是否可以像在 PHP 中那樣自動初始化多維

Is auto-initialization of multi-dimensional hash array possible in Ruby, as it is in PHP?(在 Ruby 中是否可以像在 PHP 中那樣自動初始化多維哈希數組?)
本文介紹了在 Ruby 中是否可以像在 PHP 中那樣自動初始化多維哈希數組?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我習慣于在 PHP 中使用多維數組,我可以在其中分配和初始化散列

I am so used to work in PHP with multi-dimensional arrays, where I can assign and initialize a hash by

unset($a); // just to show that there is no variable $a
$a['settings']['system']['memory'] = '1 Gb';
$a['settings']['system']['disk space'] = '100 Gb';

有沒有辦法在 Ruby 中做類似的事情?或者我需要先初始化所有維度,然后再賦值.是否可以定義一個高級哈希來執行我需要的操作?你會怎么做?

Is there a way to do similar thing in Ruby? Or I need to initialize all dimensions first, and then to assign values. Is it possible to define an advanced Hash which will allow to do what I need? How would you do that?

更新

除了Douglas提出的解決方案(見下文),我還找到了一個該主題的線程,其中 Brian Schr??er 提出了 Hash 類的擴展:

In addition to the solution proposed by Douglas (see below), I have found a thread on the subject, in which Brian Schr??er has proposed an extension for the Hash class:

class AutoHash < Hash
  def initialize(*args)
    super()
    @update, @update_index = args[0][:update], args[0][:update_key] unless args.empty?
  end

  def [](k)
    if self.has_key?k
      super(k)
    else
      AutoHash.new(:update => self, :update_key => k)
    end
  end

  def []=(k, v)
    @update[@update_index] = self if @update and @update_index
    super
  end
end

它可以解決在僅請求項目值時意外創建丟失的哈希項目時的問題,例如a['key'].

It allows to solve the problem when a missing hash item is undesirably created when the item value was only requested, e.g. a['key'].

一些額外的參考資料

  1. ruby 哈希自動激活(方面)
  2. http://trevoke.net/blog/2009/11/06/auto-vivifying-hashes-in-ruby/
  3. http://www.eecs.harvard.edu/~cduan/technical/ruby/ycombinator.shtml

推薦答案

試試這個:

def hash_with_default_hash
    Hash.new { |hash, key| hash[key] = hash_with_default_hash }
end

a = hash_with_default_hash

如果鍵不存在,則塊的結果將用作默認值.在這種情況下,默認值也是一個散列,它使用散列作為其默認值.

If the key doesn't exist, then the result of the block will be used as the default value. In this case, the default value is also a hash which uses hashes as its default values.

這篇關于在 Ruby 中是否可以像在 PHP 中那樣自動初始化多維哈希數組?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Joining 2 tables in SELECT(MYSQL/PHP)(在 SELECT(MYSQL/PHP) 中加入 2 個表)
How to make lt;option selected=quot;selectedquot;gt; set by MySQL and PHP?(如何使lt;option selected=“selectedgt;由 MySQL 和 PHP 設置?)
Auto populate a select box using an array in PHP(使用 PHP 中的數組自動填充選擇框)
PHP SQL SELECT where like search item with multiple words(PHP SQL SELECT where like search item with multiple words)
json_encode produce JSON_ERROR_UTF8 from MSSQL-SELECT(json_encode 從 MSSQL-SELECT 產生 JSON_ERROR_UTF8)
MySQL ORDER BY rand(), name ASC(MySQL ORDER BY rand(),名稱 ASC)
主站蜘蛛池模板: 国产精品久久久久久久久久尿 | 999久久久国产精品 欧美成人h版在线观看 | 美女网站视频免费黄 | 午夜欧美一区二区三区在线播放 | 高清视频一区二区三区 | 久久成人久久 | 97超碰成人| 欧美久久免费观看 | www.亚洲.com | 亚洲视频免费在线观看 | 日本中文字幕日韩精品免费 | 成人黄视频在线观看 | 麻豆视频国产在线观看 | 亚洲成人精品在线 | 久久精品中文 | 亚洲免费人成在线视频观看 | 久久精品中文字幕 | 亚洲 欧美 激情 另类 校园 | 国产精品性做久久久久久 | 国产免费一区二区 | 伊人热久久 | 久久精品国产99国产精品亚洲 | 亚洲精品在线观看网站 | 精品无码久久久久久久动漫 | 国产丝袜一区二区三区免费视频 | 国产探花在线精品一区二区 | 美国黄色毛片 | 亚洲欧美日韩精品久久亚洲区 | 日韩精品久久久久 | 精品国产精品一区二区夜夜嗨 | 国产视频久久久 | 日日爱av | 91精品久久 | 羞羞的视频免费看 | 人人色视频 | 欧美日韩在线精品 | 女同久久另类99精品国产 | 久久精品| 一区二区国产精品 | 国产三区四区 | 欧美一二精品 |