ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Hash ဆိုတာ အဘိဓာန်တစ်အုပ်လိုပါပဲ၊ နေရာအစဉ်နဲ့ မရှာဘဲ key လို့ခေါ်တဲ့ တံဆိပ်နဲ့ ရှာပါတယ်။ user hash ထဲမှာ :name ကို ရှာရင် "Aung" ရပြီး :city ကို ရှာရင် "Tokyo" ရပါတယ်။ Entry တစ်ခုချင်းစီက key တစ်ခုနဲ့ value တစ်ခု တွဲထားတာဖြစ်ပြီး တစ်ခုလုံးကို curly brace ထဲ ထည့်ရေးပါတယ်။
Hash တွေက Ruby မှာ အစိတ်အပိုင်း နာမည်တပ်ထားတဲ့ ဘာကိုမဆို ဖော်ပြတဲ့ နည်းပါ၊ user profile၊ product၊ settings file ဒါမှမဟုတ် web API က ပြန်လာတဲ့ JSON လိုပေါ့။ Data ကို နာမည်နဲ့ တောင်းတာမို့ code က ကိုင်တွယ်တဲ့ data နဲ့ တူတူ ဖတ်ရပြီး user[2] အစား user[:city] လို့ ရေးရပါတယ်။
Problem → Why → Solution
ပြဿနာ: key ရှိနေတာတောင် nil ပြန်လာတယ်
Symbol key တွေနဲ့ ဆောက်ထားတဲ့ user hash တစ်ခု ရှိပြီး user["name"] လို့ string နဲ့ ဖတ်တယ်ဆိုပါစို့။ Ruby က lookup အတွက် မညည်းပါဘူး၊ တိတ်တိတ်လေး nil ပြန်ပေးပါတယ်၊ key :name နဲ့ key "name" က မတူတဲ့ အရာနှစ်ခုမို့ပါ၊ crash က နောက်တစ်ဆင့်မှာ nil ပေါ် method ခေါ်တဲ့အခါ ဖြစ်ပါတယ်။
ဒုတိယ ခက်ခဲမှုက ရေတွက်တာပါ။ စကားလုံးတစ်လုံးချင်းစီ ဘယ်နှစ်ခါ ပေါ်လဲ ရေတွက်ချင်ရင် စကားလုံးတစ်လုံးစီအတွက် ဂဏန်းတစ်ခု သိမ်းစရာ နေရာလိုပြီး မရှိသေးတဲ့ key ကို 1 ပေါင်းလို့ မရပါဘူး။ Default value မရှိရင် increment တိုင်းကို if/else နဲ့ ဝန်းရံပြီး ရေးရပါတယ်။
user = { name: "Aung", city: "Tokyo" }
puts user["name"].upcase # NoMethodError: undefined method 'upcase' for nilKey မရှိရင် Ruby က ဘာကြောင့် မညည်းတာလဲ
Hash lookup က key မရှိတာကို ပုံမှန်အားဖြင့် မတိုင်ပါဘူး၊ [] က nil ဖြေပြီး nil က နောက်မှသာ ပျက်ပါတယ်။ Key ရဲ့ type ကလည်း သူ့ identity ရဲ့ အစိတ်အပိုင်းမို့ :name၊ "name" နဲ့ 1 က မတူတဲ့ key သုံးခုပါ။ ဒါကို သိရင် key တစ်ခု တကယ်ရှိရမယ်ဆိုတဲ့နေရာမှာ ကျယ်ကျယ်လောင်လောင် ပျက်တဲ့ ဖတ်နည်းကို ရွေးလို့ ရပြီး key မရှိတာက သဘာဝကျတဲ့ အဓိပ္ပာယ် ရှိတဲ့နေရာမှာ if/else ကို ဖယ်ပေးတဲ့ default value ကို သုံးလို့ ရပါတယ်။
Core concept: key တစ်ခုစီကို value တစ်ခုနဲ့ တွဲထားတယ်
Hash ကို curly brace နဲ့ key: value အတွဲတွေနဲ့ ရေးပါတယ်။ name: "Aung" ပုံစံက :name => "Aung" ရဲ့ shortcut ဖြစ်လို့ key က symbol ပါ၊ symbol ဆိုတာ Ruby က အရမ်းမြန်မြန် နှိုင်းယှဉ်နိုင်တဲ့ ပြောင်းလို့မရတဲ့ တံဆိပ်သေးသေးလေးပါ။ String ဒါမှမဟုတ် ဂဏန်းကိုလည်း key အဖြစ် သုံးလို့ရပေမယ့် ပုံသေ field နာမည်တွေအတွက် symbol key က ထုံးတမ်းဖြစ်ပြီး ဒီနှစ်မျိုးက အပြန်အလှန် မလဲလှယ်နိုင်ပါဘူး၊ user[:name] က entry ကို တွေ့ပေမယ့် user["name"] က မတွေ့ပါဘူး။
ဖတ်၊ ရေး၊ ဖျက်တာတွေက bracket တူတူကို သုံးပါတယ်၊ user[:city] က ဖတ်တယ်၊ user[:city] = "Osaka" က ထည့်တယ် ဒါမှမဟုတ် အစားထိုးတယ်၊ user.delete(:city) က ဖယ်ပါတယ်။ Hash က key တွေကို ပထမဆုံး ထည့်ခဲ့တဲ့ အစဉ်ကို မှတ်ထားပြီး each၊ map နဲ့ keys တို့က အဲဒီအစဉ်အတိုင်း သွားပါတယ်။ ရှိပြီးသား key ကို update လုပ်ရင် နေရာမပြောင်းပေမယ့် key တစ်ခုကို ဖျက်ပြီး ပြန်ထည့်ရင်တော့ အဆုံးကို ရောက်သွားပါတယ်။
မရှိတဲ့ key ကို ဖတ်တာက hash တွေ array တွေနဲ့ အကွာဆုံး နေရာမို့ table မှာ ရွေးစရာတွေကို စာရင်းပြထားပါတယ်။ ကိုယ့်ရည်ရွယ်ချက်နဲ့ ကိုက်တဲ့ ပျက်ပုံရှိတဲ့ ဟာကို ရွေးပါ။
| ဖတ်နည်း | Key မရှိရင် | ဘယ်အချိန်သုံးမလဲ |
|---|---|---|
| h[:key] | nil | key မရှိတာ ပုံမှန်ဖြစ်တဲ့အခါ |
| h.fetch(:key) | KeyError (key not found: :key) | key ရှိရမယ့်အခါ |
| h.fetch(:key, default) | ပေးထားတဲ့ default | အစားထိုးတန်ဖိုး ရတဲ့အခါ |
| h.dig(:a, :b) | အဆင့်တစ်ခုခု မရှိရင် nil | nested data ဖတ်တဲ့အခါ |
Default value က မရှိတဲ့ key အတွက် [] ပြန်ပေးတာကို ပြောင်းပေးပါတယ်။ Hash.new(0) က ရိုးရာ counter ဖြစ်ပြီး counts[word] += 1 က မမြင်ဖူးသေးတဲ့ စကားလုံးအတွက်တောင် အလုပ်လုပ်ပါတယ်၊ မရှိတာကို ဖတ်ရင် 0 ရလို့ပါ။ ပြောင်းလို့ရတဲ့ default ကိုတော့ သတိထားပါ၊ Hash.new([]) က မရှိတဲ့ key တိုင်းအတွက် array တစ်ခုတည်းကို ပေးပြီး ဖတ်ရုံနဲ့ ဘာမှ မသိမ်းလို့ hash ကိုယ်တိုင်က ဗလာပဲ ဖြစ်နေပါတယ်။ List တွေအတွက်တော့ key တစ်ခုချင်းစီမှာ array အသစ် ဆောက်ပြီး သိမ်းပေးတဲ့ block form Hash.new { |hash, key| hash[key] = [] } ကို သုံးပါ။
နောက်ဆုံးအနေနဲ့ Ruby 3.4 နဲ့ နောက်ပိုင်းက symbol key hash ကို {name: "Aung", "k" => 1} လို print ထုတ်ပြီး တခြား key တွေမှာ => ရဲ့ နှစ်ဖက်မှာ space ထည့်ပါတယ်၊ ဒီမှာ ပြထားတဲ့ ဥပမာတွေက Ruby 4.0.9 က ထွက်တာပါ။ ဗားရှင်းအဟောင်းတွေက အဲဒီ hash ကို {:name=>"Aung", "k"=>1} လို ပြပါတယ်။ Data က အတူတူပါပဲ၊ ပြတဲ့ပုံစံပဲ ပြောင်းသွားတာပါ။
- :name လို key တစ်ခု ရွေး
- Ruby က အဲဒီ key ရဲ့ entry ကို ရှာ
- Key မရှိရင် nil၊ default၊ KeyError
- Entry တွေက ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း
Hash ဆိုတာ အံဆွဲတိုင်းမှာ တံဆိပ်ကပ်ထားတဲ့ ဗီဒိုပါ၊ တံဆိပ်က key ဖြစ်ပြီး အံဆွဲထဲက အရာက value ပါ၊ တံဆိပ်ကို မှန်အောင် ရိုက်ရပါမယ်။
အထွေထွေ ပုံစံ
hash = { key: value, "text key" => value } # symbol key shorthand, any key type
hash[:key] # value, or nil when the key is missing
hash[:key] = new_value # add or replace an entry
hash.fetch(:key) # value, or KeyError when the key is missing
hash.dig(:outer, :inner) # nested read; nil if any level is missing
counts = Hash.new(0) # missing keys read as 0 (and are not stored)
hash.transform_values { |value| new_value }key: value က symbol key အတွက် shortcut ဖြစ်ပြီး "text key" => value လို အခြား key type တွေလည်း ရပါတယ်။ [] က nil ပေးတယ်၊ fetch က မရှိရင် KeyError ပေးတယ်၊ dig က nested ကို အဆင့်ဆင့် လုံခြုံစွာ ဖတ်တယ်၊ Hash.new(0) က counter ပေးတယ်။
Example: user profile hash တစ်ခု
ဒီ program က user profile တစ်ခုကို symbol key သုံးခုနဲ့ ဆောက်ပြီး တစ်ခုကို ဖတ်တယ်၊ နောက်ထပ် entry တစ်ခု ထည့်တယ်၊ entry အားလုံးကို loop နဲ့ print ထုတ်ပါတယ်။ နောက်ဆုံး နှစ်ကြောင်းက မရှိတဲ့ key ရဲ့ ရလဒ်ကိုပြပြီး hash တစ်ခုလုံးကို inspect ပုံစံနဲ့ ပြပါတယ်။
user = {
name: "Aung",
role: "developer",
city: "Tokyo"
}
puts user[:name]
user[:language] = "Ruby"
user.each do |key, value|
puts "#{key}: #{value}"
end
p user[:email]
p userCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Literal
name: "Aung",name: "Aung" က symbol key :name နဲ့ value "Aung" ကို တွဲပေးပါတယ်။ Entry တွေကို comma နဲ့ ခြားပြီး line တစ်ခုစီ ရေးရင် hash ရှည်ကြီးကိုပါ ဖတ်ရလွယ်ပါတယ်။
- Read
puts user[:name]Bracket ထဲမှာ symbol ကို ထည့်ပြီး ဖတ်ပါတယ်၊ Aung ကို print ထုတ်ပါတယ်။ user["name"] လို့ ရေးရင် key မတူလို့ nil ရမှာပါ။
- Add
user[:language] = "Ruby"မရှိသေးတဲ့ key ကို assign လုပ်ရင် entry အသစ်ကို အဆုံးမှာ ထည့်ပါတယ်။ ရှိပြီးသား key ဆိုရင်တော့ value ကို နေရာမပြောင်းဘဲ အစားထိုးပါတယ်။
- Iterate
user.each do |key, value|each က entry တစ်ခုချင်းစီအတွက် key နဲ့ value နှစ်ခုလုံးကို block ထဲ ပေးပါတယ်၊ ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း သွားလို့ language က နောက်ဆုံးမှာ ပေါ်ပါတယ်။
- Missing key
p user[:email]:email မရှိတဲ့အတွက် error မပေးဘဲ nil ပြန်ပေးပါတယ်။ ဒီ nil ကို မစစ်ဘဲ ဆက်သုံးရင် နောက်တစ်ဆင့်မှာ NoMethodError ဖြစ်နိုင်ပါတယ်။
- Inspect
p userp က hash ကို inspect ပုံစံနဲ့ ပြပါတယ်။ Ruby 3.4 နဲ့ နောက်ပိုင်းမှာ {name: "Aung", ...} ပုံစံ ဖြစ်ပြီး ဗားရှင်းအဟောင်းတွေက {:name=>"Aung", ...} လို ပြပါတယ်။ ဒီ output က Ruby 4.0.9 က ထွက်တာပါ။
Aung
name: Aung
role: developer
city: Tokyo
language: Ruby
nil
{name: "Aung", role: "developer", city: "Tokyo", language: "Ruby"}Default value နဲ့ ရေတွက်ခြင်း၊ shared default ထောင်ချောက်
Hash.new(0) နဲ့ စကားလုံးရေတွက်တာက if/else လုံးဝ မလိုပါဘူး။ နောက်ပိုင်းက code က Hash.new([]) မှာ ဘာမှားလဲ၊ block form က ဘယ်လို ပြင်ပေးလဲ ပြပါတယ်။ ဖတ်ပြီးတဲ့အခါ ဘာမှ မသိမ်းဘူးဆိုတဲ့ အချက်ကို သတိပြုပါ၊ ဒါကြောင့် shared ထဲက {} က ဗလာပဲ ထွက်တာပါ။
words = %w[tea milk tea sugar tea milk]
counts = Hash.new(0)
words.each { |word| counts[word] += 1 }
p counts
p counts["honey"]
p counts.key?("honey")
shared = Hash.new([])
shared[:a] << 1
shared[:b] << 2
p shared
p shared[:c]
grouped = Hash.new { |hash, key| hash[key] = [] }
grouped[:a] << 1
grouped[:b] << 2
p groupedOutput က {"tea" => 3, "milk" => 2, "sugar" => 1}၊ 0၊ false၊ {}၊ [1, 2] နဲ့ {a: [1], b: [2]} ပါ။ counts["honey"] က 0 ကို ပေးပေမယ့် "honey" ကို မသိမ်းထားလို့ key? က false ပါ။ shared မှာတော့ << က default array ကိုပဲ ပြောင်းလို့ [:a] နဲ့ [:b] နှစ်ခုစလုံးက 1 နဲ့ 2 ကို အဲဒီ array တစ်ခုတည်းထဲ ထည့်ကြပြီး hash ကတော့ ဗလာအတိုင်း ကျန်ပါတယ်။
လုံခြုံစွာ ဖတ်ခြင်း: fetch နဲ့ dig
Nested hash တွေကို config[:db][:host] လို bracket ချင်း ဆက်ဖတ်တာက အလယ်က အဆင့်တစ်ခု မရှိရင် ပျက်ပါတယ်။ dig က ဒီပြဿနာကို ဖြေရှင်းပြီး fetch ကတော့ ရှိရမယ့် key အတွက် ရှင်းလင်းတဲ့ error ပေးပါတယ်။ ဒီ code မှာ nil တန်ဖိုးတစ်ခုပါတဲ့ :debug ကိုလည်း ထည့်ထားတာ သတိထားပါ။
config = { db: { host: "localhost", ports: [5432, 5433] }, debug: nil }
p config[:db][:host]
p config.dig(:db, :ports, 1)
p config.dig(:cache, :host)
begin
config[:cache][:host]
rescue NoMethodError => e
puts e.message
end
begin
config.fetch(:timeout)
rescue KeyError => e
puts e.message
end
p config.fetch(:timeout, 30)
p config.fetch(:debug)
p config.key?(:debug)
p config[:missing_key]Output က "localhost"၊ 5433၊ nil၊ undefined method '[]' for nil၊ key not found: :timeout၊ 30၊ nil၊ true နဲ့ nil ပါ။ :debug ရဲ့ value က nil ဖြစ်လို့ fetch(:debug) က nil ပေးတာဖြစ်ပြီး key? က true ပါ၊ [:missing_key] ကလည်း nil ပေးပါတယ်။ ဒါကြောင့် [] က nil ပေးတာနဲ့ key မရှိဘူးလို့ မဆုံးဖြတ်ပါနဲ့၊ key ရှိမရှိ သိချင်ရင် key? ကို သုံးပါ။
Hash ကို ပြောင်းခြင်းနဲ့ အစဉ်ကို ထိန်းခြင်း
Hash မှာလည်း array လိုပဲ select၊ reject၊ map၊ sort_by တို့ကို သုံးလို့ရပြီး block ကို |key, value| အဖြစ် ခွဲပေးပါတယ်။ select နဲ့ reject က hash အသစ်ကို ပြန်ပေးတယ်၊ map က array ကို ပြန်ပေးတယ်၊ transform_values က key တွေကို မပြောင်းဘဲ value တွေကိုပဲ ပြောင်းတယ်၊ sort_by က [key, value] array ကို ပေးလို့ hash ပြန်လိုချင်ရင် to_h ကို ဆက်ခေါ်ပါ။
prices = { tea: 2.5, coffee: 3.0, milk: 1.2 }
p prices.transform_values { |price| (price * 1.1).round(2) }
p prices.select { |_name, price| price > 2 }
p prices.reject { |_name, price| price > 2 }
p prices.map { |name, price| "#{name}=#{price}" }
p prices.sort_by { |_name, price| price }.to_h
p prices.min_by { |_name, price| price }
p prices.sum { |_name, price| price }
prices.delete(:tea)
prices[:tea] = 2.6
p prices.keysထွက်လာတာတွေက {tea: 2.75, coffee: 3.3, milk: 1.32}၊ {tea: 2.5, coffee: 3.0}၊ {milk: 1.2}၊ ["tea=2.5", "coffee=3.0", "milk=1.2"]၊ {milk: 1.2, tea: 2.5, coffee: 3.0}၊ [:milk, 1.2]၊ 6.7 နဲ့ [:coffee, :milk, :tea] ပါ။ နောက်ဆုံးက insertion order ရဲ့ သက်သေပါ၊ :tea ကို ဖျက်ပြီး ပြန်ထည့်လိုက်လို့ အစဉ်ထဲမှာ အဆုံးကို ရောက်သွားပါတယ်။
လက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Hash တွေက web ရဲ့ အလုပ်လုပ်တဲ့ format ပါ။ JSON.parse က JSON object ကို hash အဖြစ် ပြောင်းပေးတယ် (string key နဲ့၊ symbolize_names: true ပေးရင် symbol key နဲ့)၊ YAML ကနေ load လုပ်တဲ့ configuration တွေက nested hash အဖြစ် ပြန်လာတယ်၊ Rails ကလည်း request parameter တွေကို hash နဲ့ တူတဲ့ params object နဲ့ ပေးပါတယ်။ အပြင်က data ရဲ့ ပုံသဏ္ဌာန်ကို ဘယ်တော့မှ အာမမခံနိုင်လို့ dig နဲ့ fetch နဲ့ လုံခြုံစွာ ဖတ်တာက နေ့စဉ် အကျင့်ပါ။
ရေတွက်ခြင်းနဲ့ အုပ်စုဖွဲ့တဲ့ report တွေက ဒီသင်ခန်းစာက ပုံစံတွေကို သုံးပါတယ်၊ status တစ်ခုချင်း order ဘယ်နှစ်ခုဆိုတာအတွက် Hash.new(0)၊ customer တစ်ယောက်ချင်း order list အတွက် group_by ဒါမှမဟုတ် block ပါတဲ့ Hash.new၊ count တွေကို ရာခိုင်နှုန်းဖြစ်အောင် transform_values ပါ။ Memoization ကလည်း အသုံးများပါတယ်၊ cache = Hash.new { |hash, id| hash[id] = load_user(id) } က user တစ်ယောက်ချင်းကို တစ်ခါပဲ တွက်ပြီး အဖြေကို မှတ်ထားပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. String key နဲ့ symbol key ကို ရောတာ
{ name: "Aung" } ကို user["name"] နဲ့ ဖတ်ရင် nil ရပါတယ်၊ key :name နဲ့ "name" က မတူလို့ပါ။ ပုံသေ field တွေအတွက် symbol ကို ရွေးပြီး တသမတ်တည်း သုံးပါ၊ JSON data ကိုတော့ symbolize_names: true နဲ့ parse လုပ်ပါ ဒါမှမဟုတ် string key အတိုင်း အကုန်သုံးပါ။
2. Hash.new([]) ကို သုံးတာ
Hash.new([]) မှာ h[:a] << 1 လုပ်ရင် key အားလုံးက မျှဝေတဲ့ array တစ်ခုထဲ ထည့်ပြီး h ကတော့ {} အလွတ်ပဲ ဖြစ်နေတာ ထူးဆန်းပါတယ်။ Hash.new { |hash, key| hash[key] = [] } လို block form ကို သုံးပါ။
3. nil ကို ဖြတ်ပြီး [] ဆက်ခေါ်တာ
config[:cache][:host] မှာ config[:cache] က nil ဖြစ်နေရင် NoMethodError (undefined method '[]' for nil) ဖြစ်ပါတယ်။ config.dig(:cache, :host) သုံးရင် nil ပဲ ပြန်ပြီး မပျက်ပါဘူး။ တကယ်ရှိရမယ့် key ဆိုရင်တော့ fetch ကို သုံးပြီး ချက်ချင်း ပျက်ပါစေ။
4. Loop လုပ်နေရင်း key အသစ် ထည့်တာ
h.each { |k, v| h[:b] = 2 } လို loop ထဲမှာ key အသစ်ထည့်ရင် RuntimeError (can't add a new key into hash during iteration) ဖြစ်ပါတယ်။ ရှိပြီးသား key ကို update လုပ်တာတော့ ရပါတယ်။ ပြောင်းလဲမှုတွေကို အရင်စုပြီး loop ပြီးမှ ထည့်ပါ ဒါမှမဟုတ် hash အသစ်ကို တည်ဆောက်ပါ။
5. nil value ကို key မရှိဘူးလို့ ထင်တာ
{ debug: nil }[:debug] နဲ့ {}[:debug] နှစ်ခုလုံး nil ပေးပါတယ်၊ ဒါပေမယ့် တစ်ခုမှာ key ရှိပြီး တစ်ခုမှာ မရှိပါဘူး။ ကွာခြားချက်က အရေးကြီးရင် key? ကို သုံးပါ၊ ဒါမှမဟုတ် fetch ကို သုံးပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- ပုံသေ field တွေအတွက် symbol key ကို တသမတ်တည်း သုံးပြီး အပြင်က data ရောက်လာတဲ့နေရာမှာတင် key ပုံစံကို ပြောင်းလိုက်ပါ။
- Key ရှိရမယ်ဆိုရင် fetch ကို သုံးပါ၊ [] က nil ပေးပြီး အမှားကို နောက်မှ ပေါ်စေပါတယ်။
- ရေတွက်ရင် Hash.new(0)၊ list စုရင် block form ကို သုံးပြီး shared default ကို ရှောင်ပါ။
- ပြောင်းလဲမှုမလုပ်စေချင်တဲ့ config hash တွေကို freeze လုပ်ထားပြီး to_h နဲ့ ရလဒ်ကို hash ပြန်ပြောင်းပါ။
Mini challenge
Mini challenge: HTTP status report
Server log ထဲက status code list တစ်ခုကို ရေတွက်ပြီး အစီရင်ခံစာ ထုတ်ပါ။ ရေတွက်ဖို့ Hash.new(0) ကို သုံးပါ၊ ရာခိုင်နှုန်းတွက်ဖို့ transform_values ကို သုံးပါ၊ ပါမလာတဲ့ status ကို ကိုင်တွယ်ဖို့ fetch ကို default နဲ့ သုံးပါ။
- tally(items) က item တစ်ခုချင်းစီ ဘယ်နှစ်ခါ ပေါ်လဲ ရေတွက်ထားတဲ့ hash ကို ပြန်ပေးရမယ် (Hash.new(0) သုံးရမယ်)
- percentages(counts) က count တစ်ခုချင်းကို ရာခိုင်နှုန်း (ဒသမ တစ်နေရာ round) အဖြစ် ပြောင်းထားတဲ့ hash အသစ်ကို ပြန်ပေးရမယ် (transform_values သုံးရမယ်)
- most_common(counts) က အများဆုံးပေါ်တဲ့ key ကို ပြန်ပေးရမယ် (max_by)
- မပါတဲ့ status 418 ရဲ့ count ကို fetch(418, 0) နဲ့ print ထုတ်ပါ
def tally(items)
# TODO: count each item with Hash.new(0)
end
def percentages(counts)
# TODO: a new hash of percentages, rounded to 1 decimal (transform_values)
end
def most_common(counts)
# TODO: the key with the highest count (max_by)
end
statuses = [200, 404, 200, 500, 200, 404, 301]
# TODO: print the count of 200, the most common status, the count of 418
# and the share of 404ရလဒ်: 200 x3 most common: 200 418 count: 0 404 share: 28.6%
အရိပ်အမြွက် ကြည့်ရန်
ရာခိုင်နှုန်းက count * 100.0 / total ဖြစ်ပြီး total ကို counts.values.sum နဲ့ ရပါတယ်၊ 100.0 လို့ ရေးတာက ကိန်းပြည့် စားခြင်းကို ရှောင်ဖို့ပါ။ max_by က [key, value] ကို ပေးတဲ့အတွက် .first နဲ့ key ကို ယူပါ။
အဖြေ ကြည့်ရန်
def tally(items)
counts = Hash.new(0)
items.each { |item| counts[item] += 1 }
counts
end
def percentages(counts)
total = counts.values.sum
counts.transform_values { |count| (count * 100.0 / total).round(1) }
end
def most_common(counts)
counts.max_by { |_status, count| count }.first
end
statuses = [200, 404, 200, 500, 200, 404, 301]
counts = tally(statuses)
shares = percentages(counts)
puts "200 x#{counts[200]}"
puts "most common: #{most_common(counts)}"
puts "418 count: #{counts.fetch(418, 0)}"
puts "404 share: #{shares[404]}%"Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Hash က key: value အတွဲတွေကို ထည့်ခဲ့တဲ့ အစဉ်အတိုင်း သိမ်းပါတယ်၊ symbol key နဲ့ string key က မတူပါဘူး။
- [] က key မရှိရင် nil ပေးတယ်၊ fetch က KeyError ပေးတယ် (ဒါမှမဟုတ် default)၊ dig က nested data ကို လုံခြုံစွာ ဖတ်တယ်။
- Hash.new(0) က counter ပေးပြီး Hash.new([]) က array တစ်ခုတည်း မျှဝေတာမို့ block form ကို သုံးပါ။
- select၊ reject၊ map၊ transform_values နဲ့ sort_by.to_h တို့နဲ့ hash ကို ပြောင်းပါ၊ Ruby 3.4+ က {name: "x"} ပုံစံနဲ့ print ထုတ်ပါတယ်။
Official Ruby Documentation — Ruby Documentation