新增新的健值對
獲取key對應的value
修改健值對 `hash[key]=value`
刪除預設值
遍歷包含判斷
反轉key和value `hash.invert`
獲取拼接
內容替換 `hash.replace(other_hash)`
例子
colors =
months = hash.new
months =
puts months
months =
puts months.length
2
months =
puts months.empty?
false
months =
months.store("3", "march")
puts months
months =
months.store("3", "march")
months["4"] = "april"
puts months
months =
puts months["1"] # january
puts months["3"] # 空nil
months =
months["1"] = "new"
months["3"] = "march"
puts months
months =
months.clear
puts months
{}
months =
months.delete("1")
puts months
使用block
進行匹配
months =
months.delete_if do |key, value|
key == "1"
endputs months
months =
months.default = "default_value"
months =
months.default = "default_value"
puts months.default("1")
default_value
傳遞引數為key
和value
months =
months.each do |key, value|
puts "key: #, value: #"
end
key: 1, value: january
key: 2, value: february
傳遞引數為key
和value
months =
months.each_key do |key|
puts "key: #"
end
key: 1
key: 2
傳遞引數為value
months =
months.each_value do |value|
puts "value: #"
end
value: january
value: february
months =
res = months.select
puts res
hash.has_key?(key)
hash.include?(key)
hash.key?(key)
hash.member?(key)
months =
puts months.key?("2") # true
puts months.has_key?("1") # true
puts months.include?("3") # false
puts months.member?("4") # false
方法
hash.has_value?(value)
hash.value?(value)
例子
months =
puts months.has_value?("january") # true
puts months.has_value?("march") # false
months =
puts months.invert
months =
puts months.keys
1
2
months =
puts months.values
january
february
months =
months2 =
puts months.merge(months2)
和hash.merge(other_hash)
一樣的含義
months =
months2 =
puts months.replace(months2)
Redis 雜湊hash 型別
redis hash 是乙個 string 型別的 field 和 value 的對映表,hash 特別適合用於儲存物件 基本語法 設定值 hmset hmset zhangsan name 張三 age 20 男 設定值 hset hset zhangsan name 張三 獲取資料 hgetal...
redis之雜湊型別(hash)
redis的雜湊值是字串欄位和字串值之間的對映,所以他們是表示物件的完美資料型別。在redis中的雜湊值,可儲存超過400十億鍵值對。假如我有乙個表示 職工 的物件,他可以有如下屬性 看看是如何將物件儲存到redis中的 hmset zhibin name binbin age 26 positio...
Redis的雜湊型別 Hash
redis hash 是乙個鍵值對集合。redis hash是乙個string型別的field和value的對映表,hash特別適合用於儲存物件。本次演示hash的基本操作 將雜湊表的key值test hash中的值value設為 123 lalala 333 123abc 127.0.0.1 63...