Thuta Learning
Ruby
AdvancedProgrammingအခြေခံ

Blocks & Yield

Rubyသင်ခန်းစာ 2435 မိနစ်

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • { |x| ... } နဲ့ do |x| ... end ဆိုတဲ့ block ပုံစံနှစ်ခုကို ရေးနိုင်ပြီး block ကို method ကို ပေးတဲ့အခါ ဘာဖြစ်လဲ ရှင်းပြနိုင်ဖို့
  • yield နဲ့ block_given? ကို သုံးပြီး block ရှိ/မရှိ နှစ်မျိုးလုံးကို ကိုင်တွယ်နိုင်ဖို့
  • Block က ပတ်ဝန်းကျင်က local variable တွေကို ဖမ်းထားတဲ့ closure ဖြစ်တာကို ရှင်းပြနိုင်ဖို့
  • Block ထဲက return, next, break နဲ့ lambda ထဲက return ကို ခွဲခြားပြီး LocalJumpError ကို မှန်မှန် ခန့်မှန်းနိုင်ဖို့
  • Block မပေးရင် Enumerator ပြန်တဲ့ method ကို enum_for နဲ့ ရေးနိုင်ဖို့

ရိုးရိုးလေး ရှင်းပြမယ်

Block ကို ရိုးရိုးလေး ပြောရရင်

Block ဆိုတာ method ကို ခေါ်တဲ့အခါ နောက်ကနေ ထည့်ပေးလိုက်တဲ့ code အပိုင်းအစပါ၊ { ... } ဒါမှမဟုတ် do ... end နဲ့ ရေးပါတယ်။ စားသောက်ဆိုင်မှာ စာရေးကို "ပြီးရင် ဒီအလုပ်ကို လုပ်ပေးပါ" လို့ စာရွက်ကလေး ပေးလိုက်တာနဲ့ တူပါတယ်။ Method က ဘယ်အချိန်၊ ဘယ်နှစ်ကြိမ်၊ ဘာတွေနဲ့ အဲဒီစာရွက်ကို ဖတ်ပြီး လုပ်မလဲ ဆုံးဖြတ်ပြီး ဖတ်တဲ့ကိစ္စကို yield ဆိုတဲ့ keyword က လုပ်ပါတယ်။

ဒီပုံစံက Ruby တစ်ခုလုံးမှာ ပါဝင်ပါတယ်: each, map, File.open, Dir.mktmpdir တွေက အကုန် block ကို လက်ခံတာပါ။ method ရေးသူက "အလုပ်ရဲ့ ဘောင်" (ဖွင့်တာ၊ အချိန်တိုင်းတာ၊ ပိတ်တာ) ကို ရေးပြီး အလယ်က အပိုင်းကို ခေါ်သူကို ပေးထားလိုက်တာပါ။

Problem → Why → Solution

ပြဿနာ: ထပ်ခါတလဲလဲ ပတ်ထားတဲ့ code

Script တစ်ခုက အလုပ်တစ်ခုချင်းစီ မစခင်နဲ့ ပြီးတဲ့နောက် "[log] ... finished" လို log လိုင်းတွေ ရေးပါတယ်။ ပထမတော့ လိုင်းနှစ်ကြောင်းပဲမို့ ကူးထည့်လိုက်ပါတယ်၊ ပြီးတော့ အလုပ်ဆယ်ခုအထိ ပွားလာပါတယ်။ Log ပုံစံ ပြောင်းချင်ရင် နေရာဆယ်ခုကို ပြင်ရပြီး တစ်နေရာကို မေ့သွားရင် log တွေ မညီတော့ပါဘူး။

ပိုဆိုးတာက အလုပ်တစ်ခု ပျက်ကွက်တဲ့အခါပါ။ writing report လိုင်းက error ပေးရင် အောက်က "finished" လိုင်းကို ဘယ်တော့မှ မရောက်ပါဘူး၊ ဒါကြောင့် log က အလုပ်စပြီး မပြီးသလို ကျန်ခဲ့ပါတယ်။ အလုပ်ရဲ့ "ဘောင်" ကို တစ်နေရာတည်းမှာ ရေးပြီး အလယ်က အပိုင်းကိုသာ အလုပ်တစ်ခုချင်း ပေးနိုင်ရင် ကောင်းမှာပါ။

pasted_wrapper.rb
puts "[log] saving user"
puts "  user saved"
puts "[log] saving user finished"

puts "[log] sending email"
puts "  email sent"
puts "[log] sending email finished"

puts "[log] writing report"
raise "disk full"                       # the finished line below never runs
puts "[log] writing report finished"

Block က ဘောင်ကို တစ်ခုတည်းမှာ ထားနိုင်စေတယ်

Method တစ်ခုက ဘောင် (စတင်၊ ပြီးဆုံး log) ကို ပိုင်ပြီး block အဖြစ် ပေးလိုက်တဲ့ အလုပ်ကို အလယ်မှာ yield နဲ့ ခေါ်ပါတယ်။ ဘောင်ပြောင်းချင်ရင် method တစ်ခုပဲ ပြင်ရပါတယ်၊ ensure ထည့်ထားရင် အလုပ်က ပျက်ကွက်တာတောင် "finished" log က ပေါ်ပါလိမ့်မယ်။ ဒီ idiom ကြောင့် Ruby မှာ file ဖွင့်တာ၊ lock ယူတာ၊ transaction စတာတွေအားလုံး block ပုံစံ ရှိတာပါ။

Core concept: block, yield နဲ့ closure

Block ကို method ခေါ်ဆိုမှုနောက်မှာ တိုက်ရိုက် ကပ်ပြီး ရေးပါတယ်: တစ်လိုင်းတည်းဆိုရင် { |x| x * 2 }၊ အများကြီးဆိုရင် do |x| ... end ပါ။ ခေါ်ဆိုမှုတစ်ခုမှာ block တစ်ခုသာ ရှိနိုင်ပြီး block က သူ့ဘာသာ object မဟုတ်ပါဘူး၊ method ရဲ့ အစိတ်အပိုင်းတစ်ခုလို ပါလာတာပါ။ Method ထဲက yield က အဲဒီ block ကို run ပါတယ်၊ yield ရဲ့ argument တွေက block parameter တွေ ဖြစ်လာပြီး block ရဲ့ နောက်ဆုံး expression တန်ဖိုးက yield ရဲ့ ရလဒ်အဖြစ် ပြန်ရောက်ပါတယ်။

Block မပါဘဲ ခေါ်ထားတဲ့အခါ yield လုပ်ရင် LocalJumpError ("no block given (yield)") ပေါ်ပါတယ်။ ဒါကို ကာကွယ်ဖို့ block_given? နဲ့ အရင်စစ်ပါ၊ block မရှိရင် default တန်ဖိုးပြန်တာ၊ ဒါမှမဟုတ် Enumerator ပြန်တာတို့ လုပ်လို့ရပါတယ်။ Method ရဲ့ parameter စာရင်းမှာ &block လို့ ရေးရင် Ruby က block ကို Proc object တစ်ခုအဖြစ် ပြောင်းပေးတာမို့ သိမ်းထားတာ၊ တခြား method ဆီ ဆက်ပို့တာ လုပ်လို့ရပါတယ်၊ ခေါ်တဲ့နေရာမှာ &proc ကို ပြန်သုံးရင် block အဖြစ် ပြန်ပြောင်းပါတယ်။

Block တစ်ခုက closure ဖြစ်ပါတယ်: ရေးလိုက်တဲ့ နေရာကို ပတ်ဝန်းကျင်က local variable တွေကို မှတ်မိပြီး ဖတ်ရုံမက ပြောင်းလို့လည်း ရပါတယ်၊ ကိုယ့်ကို လက်ခံတဲ့ method က အဲဒီ variable တွေကို မမြင်ပေမယ့်ပါ။ ဒါကြောင့် [3, 4, 5].each { |n| total += n } က အပြင်က total ကို တိုးစေပါတယ်။ def နဲ့ ရေးတဲ့ method ကတော့ ပြောင်းပြန်ပါ၊ scope အသစ်နဲ့ စလို့ ပတ်ဝန်းကျင် local တွေ မမြင်ရပါဘူး။ Block parameter နာမည်က ပတ်ဝန်းကျင် variable နာမည်နဲ့ တူရင် block ထဲမှာ ကိုယ်ပိုင် variable ဖြစ်ပြီး |i; x| လို ; ရဲ့နောက်က နာမည်တွေလည်း block-local ပါ။

Control flow ကို ခွဲခြားမှတ်ထားဖို့ လိုပါတယ်။ block ထဲက next က လက်ရှိ block ခေါ်ဆိုမှုကို အဆုံးသတ်ပြီး ပေးထားတဲ့ တန်ဖိုးက block ရဲ့ ရလဒ်ဖြစ်ပါတယ်။ break ကတော့ block ကို yield လုပ်ခဲ့တဲ့ method တစ်ခုလုံးကို ရပ်ပြီး ပေးထားတဲ့ တန်ဖိုးက အဲဒီ method ရဲ့ ရလဒ်ဖြစ်ပါတယ်။ return ကတော့ block ကို ရေးထားတဲ့ method ကနေ ထွက်ပါတယ်၊ ဒါကြောင့် find_first_even လို method ထဲက each block ထဲမှာ return ရေးရင် method တစ်ခုလုံး ပြီးပါတယ်။ lambda ထဲမှာတော့ return က lambda ကိုပဲ ထွက်ပါတယ်။ ရေးထားတဲ့ method ပြီးပြီးမှ Proc ကို ခေါ်ရင် return က မသွားနိုင်လို့ LocalJumpError ပေါ်ပါတယ်။

KeywordBlock ထဲမှာLambda ထဲမှာ
nextဒီအကြိမ်ကို ပြီးဆုံးစေပြီး တန်ဖိုးက block ရလဒ်return နဲ့ တူတယ်
breakyield လုပ်တဲ့ method တစ်ခုလုံးကို ရပ်စေတယ်return နဲ့ တူတယ်
returnBlock ရေးထားတဲ့ method ကနေ ထွက်တယ်Lambda ကိုပဲ ထွက်တယ်
မှတ်ထားရမယ့် ပုံစံ: စာရွက်ပေးတဲ့ စာရေး
  1. Method ခေါ်ဆိုမှုက block ကို ယူလာ
  2. Method က yield နဲ့ block ကို run
  3. Block ရဲ့ နောက်ဆုံးတန်ဖိုး ပြန်ရ
  4. Block က ပတ်ဝန်းကျင် variable တွေကို မြင်

Block ရဲ့ တန်ဖိုးက yield ရဲ့ ရလဒ်ဖြစ်ပြီး block ထဲက variable တွေကတော့ ရေးထားတဲ့ နေရာက scope ကို ဆက်သုံးပါတယ်။

အထွေထွေ ပုံစံ

ruby
def method_name(arg)
  return enum_for(:method_name, arg) unless block_given?

  result = yield(arg)             # run the caller's block with a value
  result
end

method_name(1) { |value| value + 1 }       # one-line block
method_name(1) do |value|                  # multi-line block
  value + 1
end

Method ထဲမှာ block_given? နဲ့ စစ်ပြီး block မရှိရင် Enumerator ပြန်ပါတယ်၊ yield က block ကို ခေါ်ပြီး ရလဒ်ကို ပြန်ယူပါတယ်။ ခေါ်တဲ့ဘက်မှာ တစ်လိုင်းဆိုရင် { }၊ အများကြီးဆိုရင် do ... end ကို သုံးပါတယ်။

Example: with_banner မှာ yield, closure နဲ့ block_given?

with_banner က ခေါင်းစဉ်လိုင်းနှစ်ကြောင်းရဲ့ အလယ်မှာ block ကို run ပြီး block ရဲ့ ရလဒ်ကို ပြန်ပေးတဲ့ ဘောင်ပါ။ Block ကို yield(title.length) လို့ တန်ဖိုးတစ်ခုနဲ့ ခေါ်လို့ block ရဲ့ |width| က အဲဒါကို ရပါတယ်။ Block ထဲမှာ အပြင်က total ကို ပြောင်းထားတာ (closure) နဲ့ block မပါဘဲ ခေါ်တာ ၂ ခုလုံးကို ကြည့်ပါ။

with_banner.rb
def with_banner(title)
  return "no work given for #{title}" unless block_given?

  puts "== #{title} =="
  result = yield(title.length)
  puts "== done =="
  result
end

total = 0
answer = with_banner("totals") do |width|
  [3, 4, 5].each { |n| total += n }
  total * width
end

puts answer
puts total
puts with_banner("empty")

Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်

  1. Guardreturn "no work given for #{title}" unless block_given?

    Block မပါဘဲ ခေါ်ရင် yield မလုပ်ဘဲ စာသားတစ်ခု ပြန်ပါတယ်။ ဒီစစ်ဆေးမှု မရှိရင် yield မှာ LocalJumpError ပေါ်မှာပါ။

  2. yield with a valueresult = yield(title.length)

    Block ကို ခေါ်ပြီး title.length (6) ကို ပေးလိုက်ပါတယ်။ Block ရဲ့ နောက်ဆုံး expression က yield ရဲ့ ရလဒ်ဖြစ်ပြီး result ထဲ ရောက်ပါတယ်။

  3. The call with a blockanswer = with_banner("totals") do |width|

    do |width| ... end က ခေါ်ဆိုမှုနောက်မှာ ကပ်လာတဲ့ block ပါ။ |width| က yield ကပေးတဲ့ 6 ကို လက်ခံပါတယ်။ answer ကတော့ with_banner ပြန်တဲ့ တန်ဖိုးကို ရပါတယ်။

  4. A closure at work[3, 4, 5].each { |n| total += n }

    ဒီအတွင်း block က အပြင်က total variable ကို တိုးပါတယ်။ Block က ရေးထားတဲ့ နေရာက scope ကို မှတ်ထားလို့ with_banner ထဲမှာ မဟုတ်ဘဲ ခေါ်သူရဲ့ total ကို ပြောင်းတာပါ။

  5. The block's valuetotal * width

    Block ရဲ့ နောက်ဆုံး expression ပါ: 12 * 6 = 72 ဖြစ်ပြီး yield ရဲ့ ရလဒ်၊ ပြီးတော့ with_banner ရဲ့ ပြန်တန်ဖိုး၊ နောက်ဆုံး answer ဖြစ်လာပါတယ်။ ဒါကြောင့် output မှာ 72 ပေါ်ပါတယ်။

  6. No block givenputs with_banner("empty")

    Block မပါလို့ block_given? က false ဖြစ်ပြီး method က ရှေ့ဆုံးမှာတင် စာသားပြန်ပါတယ်၊ banner လိုင်းတွေ မထွက်ပါဘူး။

You should see
== totals ==
== done ==
72
12
no work given for empty
with_banner("totals") { ... } ရဲ့ လုပ်ငန်းစဉ်
  1. Block ပါတဲ့ with_banner("totals") ကို ခေါ်
  2. block_given? စစ်ပြီး ဖွင့်လိုင်းကို print
  3. yield(6) က block ထဲကို ခုန်ဝင်
  4. Block ပြီးရင် နောက်ဆုံးတန်ဖိုး (72) ပြန်လာ
  5. ပိတ်လိုင်းကို print ပြီး 72 ကို ပြန်ပေး

Block ပါရင်

The frame prints, the block runs, its value is returned

Block မပါရင်

block_given? is false: the guard returns early

&block၊ Proc နဲ့ Symbol#to_proc

Method ရဲ့ နောက်ဆုံး parameter အဖြစ် &block လို့ ရေးရင် Ruby က ပေးထားတဲ့ block ကို Proc object တစ်ခု အဖြစ် ပြောင်းပေးပါတယ်။ ဒါကို block.call(...) နဲ့ ခေါ်လို့ရတယ်၊ ပြောင်းထားတဲ့ Proc က lambda မဟုတ်လို့ block.lambda? က false ပါ။ ခေါ်ဆိုတဲ့နေရာမှာ &block ကို ရေးရင် Proc ကို block အဖြစ် ပြန်ပို့ပေးတာမို့ pass_along က ရလာတဲ့ block ကို items.map ဆီ ဆက်ပို့ပါတယ်။ &:upcase လို Symbol ကို & နဲ့ ရေးရင်လည်း Ruby က Symbol#to_proc နဲ့ block တစ်ခု ဖန်တီးပေးပါတယ်။ Ruby 3.4 နဲ့ အထက်မှာ block ရဲ့ ပထမ parameter ကို it လို့ ရိုးရိုးခေါ်လို့ရတာကိုလည်း သတိပြုပါ။

block_to_proc.rb
def apply_twice(value, &block)
  p block.class                     # Proc
  p block.lambda?                   # false
  block.call(block.call(value))
end

p apply_twice(3) { |n| n * 2 }      # 12

def pass_along(items, &block)
  items.map(&block)                 # & turns the Proc back into a block
end

p pass_along([1, 2, 3]) { |n| n + 10 }       # [11, 12, 13]
p %w[ruby blocks].map(&:upcase)              # ["RUBY", "BLOCKS"]
p [1, 2, 3].map { it * 3 }                   # [3, 6, 9]
p [[1, 2], [3, 4]].map { |a, b| a * b }      # [2, 12]

Closure တွေက ပတ်ဝန်းကျင်ကို မှတ်မိတယ်

make_counter က count variable ကို ဖန်တီးပြီး -> { count += 1 } ဆိုတဲ့ lambda ကို ပြန်ပေးပါတယ်။ make_counter ပြီးသွားတာတောင် lambda က count ကို ဆက်ကိုင်ထားလို့ ခေါ်တိုင်း တိုးပါတယ်၊ ပြီးတော့ make_counter ကို ခေါ်တိုင်း count အသစ်တစ်ခုရတာမို့ first နဲ့ second က သီးခြားစီ ရေတွက်ပါတယ်။ ပြောင်းပြန်အနေနဲ့ def နဲ့ ရေးတဲ့ no_access ကတော့ အပြင်က total ကို မမြင်လို့ NameError ပေးပါတယ်၊ ဒါက block နဲ့ method ကြား အဓိက ကွာခြားချက်ပါ။

closures.rb
def make_counter
  count = 0
  -> { count += 1 }
end

first = make_counter
second = make_counter
first.call
first.call
p first.call                        # 3
p second.call                       # 1: each closure keeps its own count

total = 0
[1, 2, 3].each { |n| total += n }   # a block can change an outer local
p total                             # 6

def no_access
  total                             # a def starts with a clean scope
end

begin
  no_access
rescue NameError => e
  puts e.message                    # undefined local variable or method 'total' for main
end

x = 10
[1].each { |i; x| x = 99 }          # |i; x| makes x local to the block
p x                                 # 10

Block ထဲက return, next, break

အောက်က code က ဇယားထဲက စည်းမျဉ်းတွေကို တစ်ခုချင်း ပြပါတယ်။ first_even ထဲက block က return လုပ်လို့ first_even တစ်ခုလုံးက 4 ကို ပြန်ပါတယ်။ next 0 က ဒီအကြိမ်ကို 0 နဲ့ ပိတ်ပြီး map ဆက်သွားပါတယ်။ break n * 10 ကတော့ each တစ်ခုလုံးကို ရပ်ပြီး 30 ကို ပြန်ပေးပါတယ်၊ ဒါကြောင့် each ရဲ့ ပုံမှန်ရလဒ် ([1, 2, 3, 4]) ကို အစားထိုးပါတယ်။ lambda_return မှာ lambda ကိုပဲ ထွက်လို့ method က ရှေ့ဆက်ပြီး စာသားပြန်ပါတယ်၊ proc_return မှာတော့ method တစ်ခုလုံးက ထွက်သွားပါတယ်။ make_proc ကို ခေါ်ပြီး ပြန်ရတဲ့ Proc ကို နောက်မှ call လုပ်ရင် return ဆိုတဲ့ ထွက်စရာ method မရှိတော့လို့ LocalJumpError ပေါ်ပါတယ်။

block_control_flow.rb
def first_even(numbers)
  numbers.each do |n|
    return n if n.even?             # returns from first_even, not just the block
  end
  nil
end
p first_even([1, 3, 4, 6])          # 4

p [1, 2, 3, 4].map { |n| next 0 if n.odd?; n }       # [0, 2, 0, 4]
p [1, 2, 3, 4].each { |n| break n * 10 if n == 3 }   # 30

def lambda_return
  l = -> { return :from_lambda }
  value = l.call
  "after lambda: #{value}"
end
p lambda_return                     # "after lambda: from_lambda"

def proc_return
  pr = proc { return :from_proc }
  pr.call
  "never reached"
end
p proc_return                       # :from_proc

def make_proc = proc { return 1 }
begin
  make_proc.call
rescue LocalJumpError => e
  puts e.message                    # unexpected return
end

Block မပါရင် Enumerator ပြန်ပါ

Ruby ရဲ့ ကိုယ်ပိုင် iterator method တွေက block မပါဘဲ ခေါ်ရင် Enumerator ပြန်ပါတယ်: [1, 2].each, [1, 2].map တို့က အဲဒီလိုပါ။ ကိုယ်ရေးတဲ့ each ကိုလည်း return enum_for(:each) unless block_given? လို့ ဒီအတိုင်း ပြုမူခိုင်းနိုင်ပါတယ်။ အကျိုးက Enumerator ကို e.next နဲ့ တစ်ခုချင်း ဆွဲယူလို့ရတယ်၊ အဆုံးရောက်ရင် StopIteration ပေါ်ပါတယ်၊ with_index, each_slice လို method တွေနဲ့ ဆက်စပ်လို့ရတယ်။ Enumerable ကို include လုပ်ပြီး each ကို ပေးထားရင် sort, map, first စတာတွေ အကုန် အလိုအလျောက် ရပါတယ်။

enumerator_return.rb
class Bag
  include Enumerable

  def initialize(*items)
    @items = items
  end

  def each
    return enum_for(:each) unless block_given?

    @items.each { |item| yield item }
    self
  end
end

bag = Bag.new(3, 1, 2)
e = bag.each
p e.class                           # Enumerator
p e.next, e.next, e.next            # 3, 1, 2
begin
  e.next
rescue StopIteration => ex
  puts ex.message                   # iteration reached an end
end
p bag.sort                          # [1, 2, 3]
p bag.map { |n| n * 2 }             # [6, 2, 4]
p bag.each_slice(2).to_a            # [[3, 1], [2]]
p bag.each.with_index(1).to_a       # [[3, 1], [1, 2], [2, 3]]

လက်တွေ့ project တွေမှာ

Real project တွေမှာ ဘယ်လို သုံးလဲ

Ruby code ရဲ့ တစ်ဝက်လောက်က block ပါ။ each, map, select က collection တွေကို၊ File.open နဲ့ Dir.mktmpdir က resource တွေကို၊ Mutex#synchronize က thread-safety ကို၊ Rails ရဲ့ transaction do က database ကို block နဲ့ ဘောင်ခတ်ပါတယ်။ Test framework တွေက describe ... do နဲ့ it ... do တို့ကို၊ Rake က task :build do ကို၊ Gemfile က group :test do ကို DSL အဖြစ် block နဲ့ တည်ဆောက်ပါတယ်။

ကိုယ့် code မှာလည်း ဒီ idiom ကို အသုံးချလို့ရပါတယ်: with_timing, with_retries, with_transaction လို နာမည်တွေနဲ့ အလုပ်ရဲ့ ဘောင်ကို ပိုင်ပြီး ensure နဲ့ clean-up ကို အာမခံတဲ့ method တွေ ရေးပါ။ ဒါပေမယ့် block တွေ အလွန်အကျွံ ထပ်ထားတဲ့ callback အတွင်းမှာ control flow ကို ဖတ်ရခက်သွားတတ်လို့ block ကို တိုတိုထားပြီး နာမည်ရှိတဲ့ method ထဲ ရွှေ့ပါ။ Block ထဲမှာ return သုံးတာကိုလည်း သတိထားပါ၊ ဘာကြောင့်လဲဆိုတော့ ၎င်းက ရေးထားတဲ့ method တစ်ခုလုံးကို ထွက်စေလို့ပါ။

အမှားများ

အဖြစ်များတဲ့ အမှားတွေ

1. Block မပါတဲ့အခါ yield လုပ်မိတာ

def need; yield; end ကို need လို့ ခေါ်ရင် LocalJumpError ပေါ်ပြီး "no block given (yield)" ဆိုတဲ့ message ရပါတယ်။ yield မခေါ်ခင် block_given? နဲ့ စစ်ပါ၊ ဒါမှမဟုတ် Enumerator ပြန်တာ၊ default တန်ဖိုး ပြန်တာတွေ ရွေးပါ။

2. { } နဲ့ do ... end ရဲ့ ဦးစားပေးမှု ကွာခြားချက်

do ... end က { } ထက် ဦးစားပေးအနိမ့်ပါ၊ ဒါကြောင့် p [1, 2, 3].map do |x| x * 2 end မှာ block က map ကို မဟုတ်ဘဲ p ကို ကပ်သွားပါတယ်။ ရလဒ်က [2, 4, 6] မဟုတ်ဘဲ #<Enumerator: [1, 2, 3]:map> ထွက်ပါတယ်။ p([1, 2, 3].map { |x| x * 2 }) လို { } သုံးတာ၊ ဒါမှမဟုတ် ရလဒ်ကို variable ထဲ အရင်ထည့်တာ (result = [1, 2, 3].map do ... end) က ဘေးကင်းပါတယ်။

3. Block ရဲ့ နောက်ဆုံး expression က ရလဒ်ဖြစ်တာ မေ့တာ

[1, 2, 3].map { |x| puts x } က [nil, nil, nil] ပြန်ပါတယ်၊ puts က nil ကို ပြန်လို့ပါ။ ရလဒ်လိုချင်ရင် block ရဲ့ နောက်ဆုံးလိုင်းမှာ တန်ဖိုးကို ထားပါ။ Print ထုတ်ချင်ရုံဆိုရင်တော့ map အစား each ကို သုံးပါ။

4. Block ထဲက return က method တစ်ခုလုံးကို ထွက်စေတာ

each ထဲမှာ return ရေးရင် ဒီအကြိမ်ကိုပဲ ကျော်တာ မဟုတ်ဘဲ method တစ်ခုလုံးက ထွက်ပါတယ်၊ ကျန်တဲ့ code မ run တော့ပါဘူး။ ဒီအကြိမ်ကိုပဲ ကျော်ချင်ရင် next သုံးပါ။ Proc ကို သိမ်းပြီး နောက်မှ ခေါ်ရင် LocalJumpError ပေါ်နိုင်လို့ သိမ်းထားမယ့် code အတွက် lambda (-> { }) ကို သုံးပါ။

Best Practices

အကောင်းဆုံး အလေ့အကျင့်များ

  • တစ်လိုင်းတည်း block ကို { } နဲ့၊ အများကြီး block ကို do ... end နဲ့ ရေးပါ၊ ရလဒ်ကို သုံးမယ်ဆိုရင် { } ကို ဦးစားပေးပါ။
  • Block ကို optional ဖြစ်စေချင်ရင် block_given? နဲ့ စစ်ပါ၊ iterator ဖြစ်ရင် Enumerator ပြန်ပါ။
  • အလုပ်ရဲ့ ဘောင် (ဖွင့်/ပိတ်၊ log၊ lock) ကို method တစ်ခုထဲမှာ ထားပြီး ensure နဲ့ clean-up လုပ်ပါ။
  • Block ကို တိုတိုထားပါ၊ ရှည်လာရင် နာမည်ပေးထားတဲ့ method ထဲ ရွှေ့ပါ။

Mini challenge

Mini challenge: Inventory iterator

ကန့်သတ်ချက်ထက် နည်းတဲ့ ပစ္စည်းတွေကို yield လုပ်ပေးတဲ့ each_low နဲ့ block ကပေးတဲ့ ဂဏန်းအတိုင်း ပြန်ဖြည့်တဲ့ restock ပါတဲ့ Inventory class ကို ရေးပါ။

  • each_low(limit) က ပစ္စည်း နာမည်နဲ့ အရေအတွက်ကို yield လုပ်ရမယ်၊ block မပါရင် Enumerator ပြန်ရမယ်
  • restock(limit) က ပစ္စည်းနည်းတာတိုင်းအတွက် block ကို yield လုပ်ပြီး block ပြန်တဲ့ ဂဏန်းကို အရေအတွက်မှာ ပေါင်းထည့်ရမယ်
  • to_s က "bolt=10, nut=10, gear=9" လို ပုံစံနဲ့ ပြန်ရမယ်
  • each_low(5) ကို block နဲ့ print ထုတ်၊ Enumerator နဲ့ နာမည်တွေကို ဆွဲထုတ်၊ ပြီးရင် restock(5) { |_name, qty| 10 - qty } နဲ့ ပြန်ဖြည့်ကာ ရလဒ်ကို print ထုတ်ပါ
inventory.rb
class Inventory
  def initialize(stock)
    @stock = stock
  end

  def each_low(limit)
    # TODO: return an Enumerator when no block is given
    # TODO: yield the name and quantity of every item below the limit
  end

  def restock(limit)
    # TODO: for each low item, add the number the block returns
  end

  def to_s
    # TODO: "bolt=10, nut=10, gear=9"
  end
end

# TODO: build the inventory, print the low items, list their names, then restock

ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: low: bolt (3) low: nut (1) ["bolt", "nut"] bolt=10, nut=10, gear=9

အရိပ်အမြွက် ကြည့်ရန်

@stock.each { |name, qty| yield name, qty if qty < limit } နဲ့ စပါ။ restock ထဲမှာ hash ကို iterate လုပ်နေရင်း ပြင်တာကို ရှောင်ဖို့ each_low(limit).to_a ကို အရင် ယူပြီးမှ ပြင်ပါ၊ yield(name, qty) ရဲ့ ရလဒ်က block ပြန်တဲ့ တန်ဖိုးပါ။

အဖြေ ကြည့်ရန်
inventory.rb
class Inventory
  def initialize(stock)
    @stock = stock
  end

  def each_low(limit)
    return enum_for(:each_low, limit) unless block_given?

    @stock.each { |name, qty| yield name, qty if qty < limit }
  end

  def restock(limit)
    each_low(limit).to_a.each do |name, qty|
      @stock[name] = qty + yield(name, qty)
    end
    self
  end

  def to_s
    @stock.map { |name, qty| "#{name}=#{qty}" }.join(", ")
  end
end

inventory = Inventory.new("bolt" => 3, "nut" => 1, "gear" => 9)

inventory.each_low(5) { |name, qty| puts "low: #{name} (#{qty})" }
p inventory.each_low(5).map { |name, _qty| name }
inventory.restock(5) { |_name, qty| 10 - qty }
puts inventory

Quick Quiz 1 / 3

p [1, 2, 3].map do |x| x * 2 end ကို run ရင် ဘာထွက်မလဲ။

Quick Quiz 2 / 3

def first_even(numbers); numbers.each { |n| return n if n.even? }; :none; end ကို first_even([1, 4, 6]) လို့ ခေါ်ရင် ဘာပြန်မလဲ။

Quick Quiz 3 / 3

[1, 2, 3, 4].each { |n| break n * 10 if n == 3 } ရဲ့ တန်ဖိုးက ဘာလဲ။

မှတ်ထားရမယ့် အချက်များ

  • Block ကို { |x| ... } ဒါမှမဟုတ် do |x| ... end နဲ့ method ခေါ်ဆိုမှုနောက်မှာ ပေးပြီး method က yield နဲ့ run ပါတယ်၊ block ရဲ့ နောက်ဆုံးတန်ဖိုးက yield ရဲ့ ရလဒ်ပါ။
  • Block မပါရင် yield က LocalJumpError ပေးလို့ block_given? နဲ့ စစ်ပါ၊ iterator ဆိုရင် return enum_for(...) unless block_given? နဲ့ Enumerator ပြန်ပါ။
  • Block က ပတ်ဝန်းကျင် local variable တွေကို မှတ်ထားတဲ့ closure ပါ၊ def method ကတော့ scope အသစ်နဲ့ စပါတယ်၊ &block က block ကို Proc အဖြစ် ပြောင်းပါတယ်။
  • Block ထဲက next က ဒီအကြိမ်ကို၊ break က yield လုပ်တဲ့ method ကို၊ return က ရေးထားတဲ့ method ကို ထွက်စေပြီး lambda ထဲက return က lambda ကိုပဲ ထွက်စေပါတယ်။

Official Ruby Documentation — Ruby Documentation

နောက်ဆုံးပြင်ဆင်ရက်

Progress ကို ဤ browser (ဤစက်) တွင်သာ သိမ်းထားပါသည်။ အခြားစက်များနှင့် sync မလုပ်ပါ။

ဤသင်ခန်းစာတွင် အမှားတွေ့ပါက အကြောင်းကြားရန်သင့် email app ကို ဤသင်ခန်းစာလိပ်စာ ဖြည့်ပြီးသားဖြင့် ဖွင့်ပေးပါမည် — သင်ကိုယ်တိုင် ပို့မှသာ ပို့မည်ဖြစ်သည်။

Blocks & Yield | Thuta Learning