<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # Ruby 哈希(Hash) 哈希(Hash)是類似 "employee" =&gt; "salary" 這樣的鍵值對的集合。哈希的索引是通過任何對象類型的任意鍵來完成的,而不是一個整數索引,其他與數組相似。 通過鍵或值遍歷哈希的順序看起來是隨意的,且通常不是按照插入順序。如果您嘗試通過一個不存在的鍵訪問哈希,則方法會返回 _nil_。 ## 創建哈希 與數組一樣,有各種不同的方式來創建哈希。您可以通過 _new_ 類方法創建一個空的哈希: ``` months = Hash.new ``` 您也可以使用 _new_ 創建帶有默認值的哈希,不帶默認值的哈希是 _nil_: ``` months = Hash.new( "month" ) or months = Hash.new "month" ``` 當您訪問帶有默認值的哈希中的任意鍵時,如果鍵或值不存在,訪問哈希將返回默認值: ``` #!/usr/bin/ruby months = Hash.new( "month" ) puts "#{months[0]}" puts "#{months[72]}" ``` 這將產生以下結果: ``` month month ``` ``` #!/usr/bin/ruby H = Hash["a" => 100, "b" => 200] puts "#{H['a']}" puts "#{H['b']}" ``` 這將產生以下結果: ``` 100 200 ``` 您可以使用任何的 Ruby 對象作為鍵或值,甚至可以使用數組,所以下面的實例是一個有效的實例: ``` [1,"jan"] => "January" ``` ## 哈希內建方法 我們需要有一個 Hash 對象的實例來調用 Hash 方法。下面是創建 Hash 對象實例的方式: ``` Hash[[key =>|, value]* ] or Hash.new [or] Hash.new(obj) [or] Hash.new { |hash, key| block } ``` 這將返回一個使用給定對象進行填充的新的哈希。現在,使用創建的對象,我們可以調用任意可用的實例方法。例如: ``` #!/usr/bin/ruby $, = ", " months = Hash.new( "month" ) months = {"1" => "January", "2" => "February"} keys = months.keys puts "#{keys}" ``` 這將產生以下結果: ``` 2, 1 ``` 下面是公共的哈希方法(假設 _hash_ 是一個 Hash 對象): | 方法 | 描述 | | --- | --- | | **hash == other_hash** | 檢查兩個哈希是否具有相同的鍵值對個數,鍵值對是否相互匹配,來判斷兩個哈希是否相等。 | | **hash.[key]** | 使用鍵,從哈希引用值。如果未找到鍵,則返回默認值。 | | **hash.[key]=value** | 把 _value_ 給定的值與 _key_ 給定的鍵進行關聯。 | | **hash.clear** | 從哈希中移除所有的鍵值對。 | | **hash.default(key = nil)** | 返回 _hash_ 的默認值,如果未通過 default= 進行設置,則返回 nil。(如果鍵在 _hash_ 中不存在,則 [] 返回一個默認值。) | | **hash.default = obj** | 為 _hash_ 設置默認值。 | | **hash.default_proc** | 如果 _hash_ 通過塊來創建,則返回塊。 | | **hash.delete(key) [or] array.delete(key) { &#124;key&#124; block }** | 通過 _key_ 從 _hash_ 中刪除鍵值對。如果使用了塊 且未找到匹配的鍵值對,則返回塊的結果。把它與 _delete_if_ 進行比較。 | | **hash.delete_if { &#124;key,value&#124; block }** | 為 block 為 _true_ 的每個塊,從 _hash_ 中刪除鍵值對。 | | **hash.each { &#124;key,value&#124; block }** | 遍歷 _hash_,為每個 _key_ 調用一次 block,傳遞 key-value 作為一個二元素數組。 | | **hash.each_key { &#124;key&#124; block }** | 遍歷 _hash_,為每個 _key_ 調用一次 block,傳遞 _key_ 作為參數。 | | **hash.each_key { &#124;key_value_array&#124; block }** | 遍歷 _hash_,為每個 _key_ 調用一次 block,傳遞 _key_ 和 _value_ 作為參數。 | | **hash.each_key { &#124;value&#124; block }** | 遍歷 _hash_,為每個 _key_ 調用一次 block,傳遞 _value_ 作為參數。 | | **hash.empty?** | 檢查 hash 是否為空(不包含鍵值對),返回 _true_ 或 _false_。 | | **hash.fetch(key [, default] ) [or] hash.fetch(key) { &#124; key &#124; block }** | 通過給定的 _key_ 從 _hash_ 返回值。如果未找到 _key_,且未提供其他參數,則拋出 _IndexError_ 異常;如果給出了 _default_,則返回 _default_;如果指定了可選的 block,則返回 block 的結果。 | | **hash.has_key?(key) [or] hash.include?(key) [or] hash.key?(key) [or] hash.member?(key)** | 檢查給定的 _key_ 是否存在于哈希中,返回 _true_ 或 _false_。 | | **hash.has_value?(value)** | 檢查哈希是否包含給定的 _value_。 | | **hash.index(value)** | 為給定的 _value_ 返回哈希中的 _key_,如果未找到匹配值則返回 _nil_。 | | **hash.indexes(keys)** | 返回一個新的數組,由給定的鍵的值組成。找不到的鍵將插入默認值。該方法已被廢棄,請使用 select。 | | **hash.indices(keys)** | 返回一個新的數組,由給定的鍵的值組成。找不到的鍵將插入默認值。該方法已被廢棄,請使用 select。 | | **hash.inspect** | 返回哈希的打印字符串版本。 | | **hash.invert** | 創建一個新的 _hash_,倒置 _hash_ 中的 _keys_ 和 _values_。也就是說,在新的哈希中,_hash_ 中的鍵將變成值,值將變成鍵。 | | **hash.keys** | 創建一個新的數組,帶有 _hash_ 中的鍵。/td> | | **hash.length** | 以整數形式返回 _hash_ 的大小或長度。 | | **hash.merge(other_hash) [or] hash.merge(other_hash) { &#124;key, oldval, newval&#124; block }** | 返回一個新的哈希,包含 _hash_ 和 _other_hash_ 的內容,重寫 hash 中與 _other_hash_ 帶有重復鍵的鍵值對。 | | **hash.merge!(other_hash) [or] hash.merge!(other_hash) { &#124;key, oldval, newval&#124; block }** | 與 merge 相同,但實際上 hash 發生了變化。 | | **hash.rehash** | 基于每個 _key_ 的當前值重新建立 _hash_。如果插入后值發生了改變,該方法會重新索引 _hash_。 | | **hash.reject { &#124;key, value&#124; block }** | 為 _block_ 為 _true_ 的每個鍵值對創建一個新的 _hash_。 | | **hash.reject! { &#124;key, value&#124; block }** | 與 _reject_ 相同,但實際上 hash 發生了變化。 | | **hash.replace(other_hash)** | 把 _hash_ 的內容替換為 _other_hash_ 的內容。 | | **hash.select { &#124;key, value&#124; block }** | 返回一個新的數組,由 _block_ 返回 _true_ 的 _hash_ 中的鍵值對組成。 | | **hash.shift** | 從 _hash_ 中移除一個鍵值對,并把該鍵值對作為二元素數組返回。 | | **hash.size** | 以整數形式返回 _hash_ 的 _size_ 或 length。 | | **hash.sort** | 把 _hash_ 轉換為一個包含鍵值對數組的二維數組,然后進行排序。 | | **hash.store(key, value)** | 存儲 _hash_ 中的一個鍵值對。 | | **hash.to_a** | 從 hash 中創建一個二維數組。每個鍵值對轉換為一個數組,所有這些數組都存儲在一個數組中。 | | **hash.to_hash** | 返回 _hash_(self)。 | | **hash.to_s** | 把 _hash_ 轉換為一個數組,然后把該數組轉換為一個字符串。 | | **hash.update(other_hash) [or] hash.update(other_hash) {&#124;key, oldval, newval&#124; block}** | 返回一個新的哈希,包含 _hash_ 和 _other_hash_ 的內容,重寫 _hash_ 中與 _other_hash_ 帶有重復鍵的鍵值對。 | | **hash.value?(value)** | 檢查 _hash_ 是否包含給定的 _value_。 | | **hash.values** | 返回一個新的數組,包含 _hash_ 的所有值。 | | **hash.values_at(obj, ...)** | 返回一個新的數組,包含 _hash_ 中與給定的鍵相關的值。 |
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看