Thuta Learning
ရှာဖွေရန်
IntermediateProgrammingbeginner

Iterators (.each)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Ruby style code မှာ iterator တွေက အလွန်အရေးကြီးပါတယ်။ .each, .map, .select တို့က array/hash ထဲက item တွေကို readable ဖြစ်အောင် ကိုင်တွယ်ပေးပါတယ်။

ruby
prices = [100, 250, 400]

prices.each do |price|
  puts "Price: #{price} yen"
end

tax_included = prices.map do |price|
  price * 1.1
end

puts tax_included

each က item တစ်ခုချင်းစီကို run ပေးပါတယ်။ map က item တစ်ခုချင်းစီကို ပြောင်းလဲပြီး array အသစ်ပြန်ပေးပါတယ်။

You should see
Price: 100 yen Price: 250 yen Price: 400 yen 110.00000000000001 275.0 440.00000000000006

Info

Data အသစ်လိုချင်ရင် map၊ output ထုတ်ရုံလိုချင်ရင် each ကိုသုံးတာက စဉ်းစားရလွယ်ပါတယ်။

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Money calculation မှာ Float precision ပြဿနာရှိနိုင်ပါတယ်။ Real payment system တွေမှာ yen/cent ကို integer အဖြစ်သိမ်းတာ ပိုကောင်းပါတယ်။
Iterators (.each) | Thuta Learning