ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
ဒီ lesson က နောက်ပိုင်း chapter တွေက အယူအဆသုံးခုကို ပေါင်းထားပါတယ်။ state ပါတဲ့ class၊ block၊ ပြီးတော့ counter အဖြစ် သုံးတဲ့ hash တွေပါ။ Task တစ်ခုစီက သေးပေမယ့် ပေါင်းလိုက်ရင် တကယ့် program တွေရဲ့ အုတ်ချပ်တွေနဲ့ ဆင်တူပါတယ်။ model class၊ ကိုယ့်ကိုယ်ကို ရှာတဲ့ collection၊ counter၊ ပြီးတော့ item တစ်ခုစီကို ဘာလုပ်မလဲ caller ကို ဆုံးဖြတ်ခွင့်ပေးတဲ့ method တစ်ခုပါ။
Practice Exercises 1 မှာလိုပဲ အလေ့အကျင့်တူတူ သုံးပါ။ ကိုယ်ပိုင် solution ကို အရင်ရေး၊ output ကြိုခန့်မှန်း၊ run ပြီးမှ နှိုင်းယှဉ်ပါ။ ဒီတစ်ခါတော့ method တစ်ခုစီ ဘာပြန်ပေးလဲဆိုတာကို အထူးအာရုံစိုက်ပါ၊ ဘာလို့လဲဆိုတော့ မှန်တဲ့အမျိုးအစား (Array၊ Hash၊ Enumerator) ပြန်ပေးတာက exercise ရဲ့ တစ်ဝက်ပါ။
Problem → Why → Solution
ပြဿနာ: နမူနာမှာပဲ အလုပ်လုပ်ပြီး တိတ်တိတ်ကျတဲ့ draft
Library task တွေရဲ့ ပထမ draft က task ထဲက နမူနာအတွက်ပဲ အလုပ်လုပ်ပြီး နောက်မှ သေးငယ်ပြီး တိတ်ဆိတ်တဲ့ နည်းတွေနဲ့ ကျလေ့ရှိပါတယ်။ find_by_author က book.author == name အစား book == name လို့ နှိုင်းယှဉ်လို့ array အလွတ်ပြန်ပြီး ဘာ error မှ မပေါ်ပါဘူး။ ခေါင်းစဉ်တူ Book နှစ်ခုကို ပျော်ရွှင်စွာ နှစ်ခါထည့်ပါတယ်၊ ဘာလို့လဲဆိုတော့ == ကို သတ်မှတ်မထားရင် data တူတဲ့ object နှစ်ခုက မတူတဲ့ object တွေမို့ပါ။ Hash.new([]) နဲ့ရေးတဲ့ word_frequency က ပထမစကားလုံးအတွက် အလုပ်လုပ်သလိုပေါ်ပြီး စကားလုံးအားလုံးကို မျှဝေတဲ့ array တစ်ခုထဲ ထည့်ပါတယ်။ yield_even_numbers ကိုလည်း block မပါဘဲ ခေါ်ရင် LocalJumpError ဖြစ်ပါတယ်။
ဒါတွေက syntax error မဟုတ်ပါဘူး။ Ruby က အဓိပ္ပာယ်ရှိတဲ့ တစ်ခုခုကို လုပ်ပေးပေမယ့် ကိုယ်ဆိုလိုတာနဲ့ မတူတဲ့ ကိစ္စတွေဖြစ်ပြီး ကာကွယ်နည်းက ထူးဆန်းတဲ့ input တွေကို run ကြည့်ဖို့ပဲ ရှိပါတယ်။
class Book
attr_reader :title, :author
def initialize(title, author)
@title = title
@author = author
end
end
library = [Book.new("Ruby Basics", "Matz")]
p library.select { |book| book == "Matz" } # [] - a Book never equals a String
p Book.new("Ruby Basics", "Matz") == Book.new("Ruby Basics", "Matz") # false
counts = Hash.new([])
"ruby is fun".split.each { |word| counts[word] << word }
p counts # {} - every << changed the one shared default array
p counts["zzz"] # ["ruby", "is", "fun"]
def yield_even_numbers(numbers)
numbers.each { |n| yield n if n.even? }
end
yield_even_numbers([1, 2]) # LocalJumpError: no block given (yield)Ruby က ဘာကြောင့် မတိုင်ကြားတာလဲ
Ruby က dynamic ဖြစ်ပြီး ခွင့်လွှတ်တတ်ပါတယ်။ မဆိုင်တဲ့ object တွေကို == နဲ့ နှိုင်းယှဉ်ရင် false ပဲ ပြန်ပါတယ်၊ မရှိတဲ့ hash key က default ကို ပေးပါတယ်၊ Array ဖြစ်တဲ့ Hash default က အမြဲ အတူတူ Array ပါ။ Ruby ရဲ့ အမြင်မှာ ဘာမှမှားတာ မရှိလို့ ဘာမှ report မလုပ်ပါဘူး။ ဖြေရှင်းနည်းက method တစ်ခုစီရဲ့ စာချုပ် (ဘာလက်ခံလဲ၊ ဘာကို အမြဲပြန်ပေးလဲ) ကို ဆုံးဖြတ်ပြီး ရှုပ်ထွေးတဲ့ ကိစ္စတွေကို တမင်စမ်းဖို့ပါ။
Core concept: ရှင်းလင်းတဲ့ စာချုပ်ပါတဲ့ class၊ block နဲ့ counter
Task လေးခုလုံးမှာ အလေ့အကျင့်တစ်ခုတူပါတယ်။ body မရေးခင် method က ဘာကို ကတိပြုလဲ ဆုံးဖြတ်ပါ။ ကတိမှာ အပိုင်းသုံးပိုင်း ရှိပါတယ်။ အမြဲပြန်ပေးမယ့် တန်ဖိုးအမျိုးအစား၊ input ဗလာ ဒါမှမဟုတ် nil အတွက် ဘာလုပ်မလဲ၊ item တစ်ခုစီကို ဘယ်လိုကိုင်တွယ်မလဲ ဆုံးဖြတ်သူ ဘယ်သူလဲ ဆိုတာပါ။
၁။ Class ကို သေးငယ်ပြီး ရိုးသားတဲ့ interface ပေးပါ
Book မှာ title နဲ့ author လိုတာမို့ initialize က နှစ်ခုလုံးကို တောင်းပြီး ဗလာတွေကို ငြင်းပါတယ်။ attr_reader က setter မပါဘဲ ဖော်ပြလို့ Library နဲ့ report ကြား စာအုပ်တွေကို လုံခြုံစွာ မျှဝေလို့ရပါတယ်။ title နဲ့ author ကိုက်ရင် စာအုပ်တစ်အုပ်တည်းလို့ သတ်မှတ်ချင်ရင် အဲဒီ field တွေအပေါ်မှာ == (eql? နဲ့ hash ပါ အတူ) ကို သတ်မှတ်ပါ။ မဟုတ်ရင် data တူရင်တောင် object တိုင်းက ထူးခြားနေပါတယ်။
၂။ Collection ကို Enumerable သုံးခိုင်းပါ
Library က @books ကို private ထားပြီး each ကို သတ်မှတ်ကာ Enumerable ကို include လုပ်ပါတယ်။ ဒီအခါ find_by_author က self ပေါ်က select ဖြစ်ပြီး most_prolific_author က tally ဖြစ်လို့ method တစ်ခုမှ array ကို တိုက်ရိုက် မထိတော့ပါဘူး။ Search method တွေက ဘာမှမတွေ့ရင်တောင် Array ပြန်လို့ caller တွေက nil စစ်စရာမလိုဘဲ map ဒါမှမဟုတ် size ကို ဆက်ခေါ်နိုင်ပါတယ်။
၃။ Default လုံခြုံတဲ့ counter ကို ရွေးပါ
Hash.new(0) က မရှိတဲ့ key တိုင်းကို 0 ပေးလို့ counts[word] += 1 က ပထမအကြိမ်ကတည်းက အလုပ်လုပ်ပါတယ်။ Hash.new { |hash, key| hash[key] = [] } ဆိုတဲ့ block ပုံစံက key တစ်ခုချင်းစီအတွက် list ဆောက်ဖို့ လုံခြုံတဲ့နည်းပါ၊ တစ်ခါတိုင်း array အသစ် ဖန်တီးလို့ပါ။ Hash.new([]) ကတော့ မျှဝေတဲ့ array တစ်ခုတည်းကို ပေးပါတယ်။ tally က ရေတွက်တာကို call တစ်ခုနဲ့ လုပ်ပြီး default မပါတဲ့ ရိုးရိုး Hash ပြန်ပေးပါတယ်။
၄။ အမှတ်တူရင် ဘာလုပ်မလဲ စည်းမျဉ်းချမှတ်ပါ
စာရေးဆရာနှစ်ယောက် စာအုပ်အရေအတွက်တူရင် max_by လို method က မြင်ဖူးတဲ့ ပထမတစ်ယောက်ကို တိတ်တိတ်ပြန်ပေးပြီး ဒါက insertion order ပေါ် မူတည်ပါတယ်။ စည်းမျဉ်းကို ထင်ရှားစေပါ။ [-count, name] နဲ့ sort လုပ်ရင် အများဆုံးက နိုင်ပြီး အမှတ်တူရင် အက္ခရာစဉ်ထဲ ပထမနာမည်ကို ရပါတယ်။ ဒါဆို data ဘယ်လိုရောက်လာလာ အဖြေအတူတူပါပဲ။
၅။ Caller ရဲ့ block ကို အလုပ်လုပ်ခိုင်းပါ
yield က caller ရေးထားတဲ့ block ကို item တစ်ခုစီ ပေးပါတယ်။ block မပါရင် yield က LocalJumpError ကို raise လုပ်ပါတယ်။ block_given? ကို စစ်ပြီး ရှင်းတဲ့ error ပေးပါ၊ ဒါမှမဟုတ် ပိုအကျိုးရှိအောင် to_enum နဲ့ Enumerator ပြန်ပေးလို့ caller တွေက .to_a ဒါမှမဟုတ် .map ကို ဆက်ခေါ်နိုင်ပါစေ။ Integer မဟုတ်တာတွေကို တစ်ဝက်လမ်းမှာ crash မဖြစ်စေဘဲ ကျော်ပါ။
| Method | အမြဲ ပြန်ပေးတာ | Input ဗလာ ဒါမှမဟုတ် nil |
|---|---|---|
| find_by_author(name) | Array | nil၊ ဗလာ၊ မသိတဲ့ နာမည်အတွက် [] |
| most_prolific_author | String ဒါမှမဟုတ် nil | Library အလွတ်ဆို nil |
| word_frequency(text) | String => Integer Hash | "" ဒါမှမဟုတ် nil ဆို {} |
| yield_even_numbers(list) | Block မပါရင် Enumerator၊ ပါရင် yield လုပ်ခဲ့တဲ့ အရေအတွက် | ဘာမှ yield မလုပ်၊ 0 ပြန် |
- အမြဲပြန်မယ့် အမျိုးအစား ဆုံးဖြတ်
- ဗလာနဲ့ nil input အတွက် ဆုံးဖြတ်
- အမှတ်တူ၊ ထပ်နေတာ ဆုံးဖြတ်
- Body ရေး၊ ထူးဆန်းတဲ့ input နဲ့ ခေါ်ကြည့်
Input ပေါ်မူတည်ပြီး ပြန်တဲ့ အမျိုးအစား ပြောင်းတဲ့ method (တစ်ခါ nil၊ တစ်ခါ Array) က caller တိုင်းကို စစ်ခိုင်းပါတယ်။ အမျိုးအစားကို အရင်ပုံသေလုပ်ရင် caller တွေ ရိုးရှင်းနေပါတယ်။
အခြေခံ ပုံစံ
def each_match(items, wanted)
return to_enum(:each_match, items, wanted) unless block_given?
items.each do |item|
yield item if item == wanted
end
end
counts = Hash.new(0)
words.each { |word| counts[word] += 1 }
each_match(words, "ruby") { |word| puts word }ပထမအပိုင်းက block ပါရင် item တစ်ခုစီကို yield လုပ်ပြီး block မပါရင် Enumerator ပြန်ပါတယ်။ ဒုတိယအပိုင်းက Hash.new(0) counter ကို သုံးပြီး နောက်ဆုံး line က block ကို ပေးပြီး ခေါ်ပုံပါ။
Task လေးခုအတွက် reference solution
ကိုယ်ဟာကိုယ် အရင်ကြိုးစားပြီးမှသာ နှိုင်းယှဉ်ပါ။ ဒီ solution တွေက task ထဲက နမူနာ call တွေထက် ပိုကျယ်ပါတယ်။ Book တူညီမှုနဲ့ Library က ထပ်နေတဲ့ စာအုပ်ကို ငြင်းတာ၊ မသိတဲ့ နာမည်နဲ့ nil ပါတဲ့ find_by_author၊ most_prolific_author မှာ အမှတ်တူခြင်း၊ ပုဒ်ဖြတ်၊ အကြီးအသေး၊ မြန်မာစကားလုံးတွေပါတဲ့ word_frequency၊ nil နဲ့ Float element တွေပါတဲ့ ဒါမှမဟုတ် block မပါတဲ့ yield_even_numbers တို့ကို စမ်းထားပါတယ်။ Hash output တွေက Ruby 4.0.9 ရဲ့ format နဲ့ပါ၊ ဗားရှင်းအဟောင်းတွေမှာ {"ruby"=>2} လို ပေါ်ပါမယ်။
# Task 1: a Book is read-only after creation. Two books with the same title and
# author (ignoring case) count as the same book.
class Book
attr_reader :title, :author
def initialize(title, author)
raise ArgumentError, "title can't be blank" if title.to_s.strip.empty?
raise ArgumentError, "author can't be blank" if author.to_s.strip.empty?
@title = title.strip
@author = author.strip
end
def ==(other)
other.is_a?(Book) && identity == other.identity
end
alias eql? ==
def hash = identity.hash
def to_s = "#{title} by #{author}"
protected
def identity = [title.downcase, author.downcase]
end
# Task 2: a Library keeps Books and can search them.
class Library
include Enumerable
def initialize
@books = []
end
def add_book(book)
raise ArgumentError, "already in the library: #{book}" if @books.include?(book)
@books << book
self
end
def each(&block)
return enum_for(:each) unless block
@books.each(&block)
self
end
# Always returns an Array, never nil. Unknown, blank or nil names give [].
def find_by_author(name)
wanted = name.to_s.strip.downcase
return [] if wanted.empty?
select { |book| book.author.downcase == wanted }
end
# Ties go to the author who comes first alphabetically; nil for an empty library.
def most_prolific_author
counts = map(&:author).tally
return nil if counts.empty?
counts.min_by { |author, count| [-count, author] }.first
end
end
# Task 3: count each word. Hash.new(0) makes a missing word start at 0.
def word_frequency(text)
counts = Hash.new(0)
text.to_s.downcase.scan(/[\p{L}\p{M}\p{N}']+/) { |word| counts[word] += 1 }
counts
end
# Task 4: yield only the even Integers and return how many were yielded.
# Without a block it returns an Enumerator instead of raising LocalJumpError.
def yield_even_numbers(numbers)
return to_enum(:yield_even_numbers, numbers) unless block_given?
count = 0
numbers.each do |n|
next unless n.is_a?(Integer) && n.even?
yield n
count += 1
end
count
end
library = Library.new
library.add_book(Book.new("Ruby Basics", "Matz"))
.add_book(Book.new("Programming Ruby", "Dave Thomas"))
.add_book(Book.new("Ruby Under a Microscope", "Pat Shaughnessy"))
.add_book(Book.new("Eloquent Ruby", "Russ Olsen"))
.add_book(Book.new("More Ruby", "Matz"))
puts library.find_by_author("matz").map(&:to_s)
p library.find_by_author("Nobody")
p library.find_by_author(nil)
puts "Most prolific: #{library.most_prolific_author}"
p Library.new.most_prolific_author
begin
library.add_book(Book.new("RUBY BASICS", " matz "))
rescue ArgumentError => e
puts "Rejected: #{e.message}"
end
freq = word_frequency("Ruby is fun and ruby is easy!")
puts freq.map { |word, count| "#{word}=#{count}" }.join(" ")
puts freq["missing"], freq.key?("missing")
p word_frequency("")
p word_frequency("မြန်မာစာ ရေးတယ် မြန်မာစာ")
yielded = yield_even_numbers([1, 2, 3, 4, 5, 6]) { |n| puts n }
puts "yielded #{yielded} numbers"
p yield_even_numbers([1, 2, 3, 4, 5, 6]).to_a
p yield_even_numbers([-4, 0, 7, nil, 2.0, "8"]).map { |n| n * 10 }
p yield_even_numbers([]).to_aCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Equality for hashes and uniq
alias eql? ==Hash key၊ uniq နဲ့ Set တွေက == မဟုတ်ဘဲ eql? နဲ့ hash ကို သုံးပါတယ်။ ဒါကြောင့် == ကို သတ်မှတ်ရင် eql? ကို alias လုပ်ပြီး def hash = identity.hash နဲ့ တူညီတဲ့ hash တန်ဖိုးကို ပေးရပါတယ်။ သုံးခုလုံး ကိုက်ညီမှ "RUBY BASICS" by " matz " ကို ရှိပြီးသား "Ruby Basics" by "Matz" အဖြစ် သိပါတယ်။
- A protected comparison key
def identity = [title.downcase, author.downcase]identity က title နဲ့ author ကို အကြီးအသေးမခွဲဘဲ နှိုင်းယှဉ်တဲ့ key ပါ။ protected ဆိုတာ တခြား Book object တွေက ခေါ်လို့ရပေမယ့် အပြင်ကလူတွေ ခေါ်မရအောင်လို့ == ထဲက other.identity က အလုပ်လုပ်ပါတယ်။
- Compare the attribute, not the object
select { |book| book.author.downcase == wanted }book == wanted လို့ရေးရင် Book နဲ့ String ကို နှိုင်းယှဉ်ရာ ဘယ်တော့မှ မညီလို့ array အလွတ်ပါ။ book.author နဲ့ ယှဉ်ပါတယ်။ wanted က strip နဲ့ downcase လုပ်ပြီးသားဖြစ်ပြီး nil.to_s က "" ဖြစ်တာကြောင့် return [] if wanted.empty? က nil၊ ဗလာနဲ့ space တွေကို တူညီစွာ ကိုင်တွယ်ပါတယ်။
- A deliberate tie-break
counts.min_by { |author, count| [-count, author] }.firstKey ကို [-count, author] လို့ ထားတာကြောင့် count အများဆုံးက ပထမ၊ အမှတ်တူရင် အက္ခရာစဉ်ထဲ နာမည်က ပထမပါ။ Matz က စာအုပ်နှစ်အုပ်နဲ့ အနိုင်ရပါတယ်။ min_by နောက်က .first က [author, count] အတွဲထဲက နာမည်ကို ယူပါတယ်။ Library အလွတ်ဆိုရင် counts က ဗလာမို့ အရင် return nil လုပ်ပါတယ်။
- Words that survive Unicode
text.to_s.downcase.scan(/[\p{L}\p{M}\p{N}']+/) { |word| counts[word] += 1 }\p{L} က စာလုံး၊ \p{M} က combining mark၊ \p{N} က ဂဏန်းတွေပါ။ \p{M} မပါရင် မြန်မာစကားလုံးတွေဟာ သရ နဲ့ အသတ်မှာ ပြတ်သွားပါတယ်။ scan ကို block နဲ့ ခေါ်လို့ array အလယ်တန်း မလိုပါဘူး။ downcase ကြောင့် "Ruby" နဲ့ "ruby" က တစ်ခုတည်း ဖြစ်ပါတယ်။
- Counting with a default
counts = Hash.new(0)Default 0 ကြောင့် freq["missing"] က 0 ပြန်ပေမယ့် key ကို မသိမ်းလို့ freq.key?("missing") က false ပါ။ freq.fetch("missing") ကတော့ KeyError ပေးပါလိမ့်မယ်။ ဒီ default က hash ရဲ့ အစိတ်အပိုင်းအဖြစ် ကျန်နေလို့ ပြန်ပေးတဲ့ hash ကို တခြား code က ဖတ်တဲ့အခါ သတိထားပါ။
- Enumerator when no block is given
return to_enum(:yield_even_numbers, numbers) unless block_given?Block မပါရင် method ကို ပြန်ခေါ်မယ့် Enumerator ကို ပြန်ပေးလို့ yield_even_numbers([1, 2, 3]).to_a ဒါမှမဟုတ် .map ကို ခေါ်လို့ရပါတယ်။ Block ပါရင် ဆက်သွားပြီး Integer မဟုတ်တဲ့ element (nil၊ 2.0၊ "8") ကို next နဲ့ ကျော်ကာ yield လုပ်ခဲ့တဲ့ အရေအတွက်ကို ပြန်ပေးပါတယ်။
Ruby Basics by Matz
More Ruby by Matz
[]
[]
Most prolific: Matz
nil
Rejected: already in the library: RUBY BASICS by matz
ruby=2 is=2 fun=1 and=1 easy=1
0
false
{}
{"မြန်မာစာ" => 2, "ရေးတယ်" => 1}
2
4
6
yielded 3 numbers
[2, 4, 6]
[-40, 0]
[]Hash.new(0)၊ Hash.new([]) နဲ့ tally
Hash default သုံးမျိုးက တူသလိုပေမယ့် မတူညီစွာ ပြုမူပါတယ်။ Hash.new(0) က number တွေအတွက် လုံခြုံပါတယ်၊ ဘာလို့လဲဆိုတော့ number တစ်ခုကို မပြင်ဘဲ += က အသစ်ကို assign လုပ်လို့ပါ။ Array လို ပြောင်းလို့ရတဲ့ default နဲ့ကတော့ << က ပေးထားတဲ့ object ကိုယ်တိုင်ကို ပြင်ပြီး hash ထဲ ဘာမှ မသိမ်းပါဘူး။ အောက်က snippet က ထောင်ချောက်၊ block နဲ့ ပြင်နည်း၊ tally တို့ကို ဘေးချင်းကပ် ပြပါတယ်။
shared = Hash.new([])
shared[:a] << 1
shared[:b] << 2
p shared # {} - nothing was stored
p shared[:anything] # [1, 2] - the one shared default array
safe = Hash.new { |hash, key| hash[key] = [] }
safe[:a] << 1
safe[:b] << 2
p safe # {a: [1], b: [2]}
counts = %w[ruby is fun ruby].tally
p counts # {"ruby" => 2, "is" => 1, "fun" => 1}
p counts["nope"] # nil - tally returns a plain Hash with no defaultစည်းမျဉ်းတစ်ခုတည်း မှတ်ပါ။ default က ပြောင်းလို့မရတဲ့ တန်ဖိုး (number၊ nil၊ symbol) ဆိုရင် Hash.new(value) ကို သုံးပါ၊ ပြောင်းလို့ရတဲ့ object (array၊ hash၊ string) ဆိုရင် block ပုံစံကို သုံးပါ။ ရေတွက်ရုံပဲဆိုရင်တော့ tally က အတိုဆုံးပါ။
yield က တကယ်ဘာလုပ်သလဲ
yield က block ကို ခေါ်ပြီး block ရဲ့ နောက်ဆုံးတန်ဖိုးကို ပြန်ပေးပါတယ်။ map လို method တွေ အလုပ်လုပ်ပုံက ဒါပါပဲ။ ဒီအတွက် method ကိုယ်တိုင်က item တစ်ခုစီကို ဘာလုပ်မလဲ မသိဘဲ ပေးလိုက်ရုံပဲ လုပ်ပြီး ဆုံးဖြတ်ချက်ကို caller ကို ထားခဲ့ပါတယ်။ yield သုံးတဲ့ ပုံစံနဲ့ array ပြန်ပေးတဲ့ ပုံစံ နှစ်ခုက ဒီဇိုင်းမတူပါဘူး။ yield ပုံစံက array မဆောက်ဘဲ item တွေကို တစ်ခုချင်း ပို့လို့ ကြီးမားတဲ့ ဒါမှမဟုတ် အဆုံးမရှိတဲ့ data အတွက် အသုံးဝင်ပါတယ်။ Array ပြန်ပေးတာက test လုပ်ဖို့ ပိုလွယ်ပါတယ်။
def my_map(items)
return to_enum(:my_map, items) unless block_given?
result = []
items.each { |item| result << yield(item) }
result
end
p my_map([1, 2, 3]) { |n| n * 10 } # yield returns the block's value
p my_map([1, 2, 3]).with_index.to_a # no block: an Enumerator you can chainလက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
ဒီလေ့ကျင့်ခန်းတွေထဲက အရာအားလုံးက လက်တွေ့ Ruby code မှာ ပေါ်ပါတယ်။ validation နဲ့ equality ပါတဲ့ model class က Rails model တိုင်းနဲ့ domain object တိုင်းရဲ့ အခြေခံဖြစ်ပြီး == နဲ့ hash ကို မှန်မှန်ကန်ကန် သတ်မှတ်ထားမှ uniq၊ include? နဲ့ Hash key တွေ မှန်ကန်စွာ ပြုမူပါတယ်။ Enumerable ပါတဲ့ collection တွေက Ruby library တွေက array ကို မဖော်ပြဘဲ list တွေပေးတဲ့ နည်းပါ။ Hash counter တွေက log analysis၊ analytics ("URL တစ်ခုချင်းရဲ့ page view")၊ spell checker တွေမှာ ပေါ်ပါတယ်။
Block လက်ခံတဲ့ method တွေက caller ကို အပြုအမူ ပြင်ခွင့်ပေးတဲ့ Ruby ပုံစံပါ။ File.open { |f| ... } က file ကို ကိုယ့်အစား ပိတ်ပေးပါတယ်၊ Array#each၊ Dir.glob { }၊ assertion block တွေ run တဲ့ test framework တွေလည်း အတူတူပါ။ each_slice နဲ့ each_with_index လို block မပါရင် Enumerator ပြန်တဲ့ method တွေက map ဒါမှမဟုတ် with_index နဲ့ ဆက်ခေါ်ခွင့်ပေးတဲ့ နည်းပါ။ ကိုယ့် method တွေမှာလည်း အဲဒီ ထုံးစံကို လိုက်နာရင် Ruby သုံးသူတွေအတွက် အလိုလို ရင်းနှီးပါလိမ့်မယ်။
အမှားများ
ဖြစ်လေ့ရှိတဲ့ အမှားများ
Attribute အစား object ကို နှိုင်းယှဉ်တာ
find_by_author ထဲက select block မှာ book.author == name အစား book == name လို့ ရေးရင် Book နဲ့ String ကို နှိုင်းယှဉ်ရာ ဘယ်တော့မှ မညီပါဘူး။ ရလဒ်က array အလွတ်ဖြစ်ပြီး error မပေါ်ပါဘူး။ ပြီးတော့ "matz" နဲ့ "Matz" က မညီလို့ နှစ်ဖက်စလုံးကို downcase လုပ်ပြီး နှိုင်းယှဉ်ပါ။
Hash.new([]) ရဲ့ မျှဝေတဲ့ default
counts = Hash.new([]) ရေးပြီး counts[word] << word လို့ ခေါ်ရင် hash ထဲမှာ ဘာမှ မသိမ်းဘဲ default array တစ်ခုထဲကို စကားလုံးအားလုံး ပုံထားတာပါ။ counts ကို print ထုတ်ရင် {} ပေါ်ပြီး counts["မည်သည့်စာလုံး"] က စကားလုံးအားလုံးပါတဲ့ array ကို ပြန်ပေးပါတယ်။ Hash.new { |hash, key| hash[key] = [] } ကို သုံးပါ။
block_given? မစစ်ဘဲ yield လုပ်တာ
Block မပါဘဲ ခေါ်လိုက်ရင် yield အတန်းကို ရောက်တဲ့အချိန်မှာ Ruby 4.0.9 မှာ LocalJumpError: no block given (yield) ဖြစ်ပါတယ်။ ဒီအမှားက method ကို ခေါ်တဲ့ line မှာ မဟုတ်ဘဲ yield line မှာ ပေါ်ပြီး list မှာ စုံကိန်း မပါရင်တော့ ဘာ error မှ မပေါ်ဘဲ ဖုံးကွယ်နေတတ်ပါတယ်။ ထိပ်ဆုံးမှာ block_given? ကို စစ်ပြီး Enumerator ပြန်ပါ။
hash နဲ့ eql? မပါဘဲ == ကိုပဲ သတ်မှတ်တာ
Book မှာ == ကိုပဲ သတ်မှတ်ထားရင် include? က စာအုပ်တူကို မှန်ကန်စွာ တွေ့ပေမယ့် uniq က နှစ်အုပ်လုံးကို ထားပြီး Hash key အနေနဲ့ ရှာရင်လည်း မတွေ့ပါဘူး။ တစ်နေရာမှာ အလုပ်လုပ်ပြီး တစ်နေရာမှာ မလုပ်တဲ့ ဒီကိစ္စက ရှာရခက်ပါတယ်။ == ကို သတ်မှတ်တိုင်း eql? နဲ့ hash ကိုပါ တစ်ပါတည်း သတ်မှတ်ပါ။
Mini challenge
Block ပါ/မပါ ခေါင်းစဉ်ကို ရှာပါ
Library မှာ find_by_title(keyword) ကို ထည့်ပါ။ အကြီးအသေးမခွဲဘဲ ခေါင်းစဉ်ထဲ keyword ပါတဲ့ စာအုပ်တွေကို ပြန်ပေးပါတယ်။ keyword ဗလာ ဒါမှမဟုတ် nil ဆိုရင် array အလွတ်ပေးပါ။ Block ပါရင် Array ပြန်မယ့်အစား တွေ့တဲ့စာအုပ်တစ်အုပ်စီကို yield လုပ်ပြီး yield လုပ်ခဲ့တဲ့ အရေအတွက်ကို ပြန်ပေးပါ။
- ခေါင်းစဉ်ရဲ့ ဘယ်နေရာမှာမဆို ကိုက်ညီပြီး အကြီးအသေးကို လျစ်လျူရှုပါ
- Block မပါရင် အမြဲ Array ပြန်ပါ (nil မဟုတ်ရ)
- keyword ဗလာ ဒါမှမဟုတ် nil က ဘာမှ မကိုက်ညီရပါ
- Block ပါရင် library အစီအစဉ်အတိုင်း match တစ်ခုစီကို yield လုပ်ပြီး match အရေအတွက်ကို ပြန်ပါ
# Reuse Book and Library from the lesson, then add this method.
class Library
def find_by_title(keyword)
# TODO: normalise the keyword (nil and blank must match nothing)
# TODO: pick the books whose title includes it, ignoring case
# TODO: with a block, yield each match and return how many there were
# TODO: without a block, return the Array of matches
end
endရလဒ်: "ruby" ကို ရှာရင် စာအုပ်သုံးအုပ်ရဲ့ ခေါင်းစဉ် ထွက်ပြီး "RUBY" အတွက် size က 3 ပါ။ "python"၊ " " နဲ့ nil တို့က [] ပါ။ နောက်ဆုံးမှာ "design" ကို block နဲ့ ရှာရင် found: ပါတဲ့ line တစ်ကြောင်းနဲ့ yielded 1 book(s) ပေါ်ပါမယ်။
အရိပ်အမြွက် ကြည့်ရန်
keyword.to_s.strip.downcase က nil နဲ့ space တွေကို ဗလာအဖြစ် တူညီစွာ ပြောင်းပေးပါတယ်။ select နဲ့ match တွေကို Array တစ်ခုအဖြစ် အရင်စုပါ၊ ပြီးမှ block_given? ကို စစ်ပါ။ Block မပါရင် Array ကို ချက်ချင်းပြန်ပါ၊ ပါရင် each နဲ့ yield လုပ်ပြီး size ပြန်ပါ။
အဖြေ ကြည့်ရန်
# A trimmed stand-in for the lesson's Book keeps this file short.
Book = Struct.new(:title, :author)
class Library
include Enumerable
def initialize(books)
@books = books
end
def each(&block)
@books.each(&block)
end
# Without a block: an Array of matches (never nil). With a block: yields each
# match in library order and returns how many were yielded.
def find_by_title(keyword)
wanted = keyword.to_s.strip.downcase
matches = wanted.empty? ? [] : select { |book| book.title.downcase.include?(wanted) }
return matches unless block_given?
matches.each { |book| yield book }
matches.size
end
end
library = Library.new([
Book.new("Ruby Basics", "Matz"),
Book.new("Programming Ruby", "Dave Thomas"),
Book.new("Eloquent JavaScript", "Marijn Haverbeke"),
Book.new("Practical Object-Oriented Design in Ruby", "Sandi Metz"),
])
puts library.find_by_title("ruby").map(&:title)
p library.find_by_title("RUBY").size
p library.find_by_title("python")
p library.find_by_title(" ")
p library.find_by_title(nil)
count = library.find_by_title("design") { |book| puts "found: #{book.title}" }
puts "yielded #{count} book(s)"Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Method တစ်ခုစီရဲ့ ကတိ (ပြန်မယ့်အမျိုးအစား၊ ဗလာ/nil အပြုအမူ၊ အမှတ်တူစည်းမျဉ်း) ကို body မရေးခင် ဆုံးဖြတ်ပါ
- == ကို သတ်မှတ်ရင် eql? နဲ့ hash ကိုပါ တစ်ပါတည်း သတ်မှတ်ပါ၊ မဟုတ်ရင် uniq နဲ့ Hash key တွေ မညီပါဘူး
- Search method တွေက Array ပြန်ပြီး Hash.new(0) ဒါမှမဟုတ် tally ကို သုံးပါ၊ Hash.new([]) ကို ရှောင်ပါ
- yield သုံးတဲ့ method က block_given? ကို စစ်ပြီး block မပါရင် to_enum နဲ့ Enumerator ပြန်ပါ
- အမှတ်တူရင် [-count, name] လို sort key နဲ့ ရည်ရွယ်ချက်ရှိရှိ ဖြတ်ပါ
Official Ruby Documentation — Ruby Documentation