Thuta Learning
Ruby
ProjectsProgrammingအခြေခံ

Task Tracker Project - Part 2

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

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

  • Task တွေကို id နဲ့ ရှာ၊ complete လုပ်၊ ဖျက်နိုင်ပြီး id မရှိရင် TaskNotFound ဆိုတဲ့ ကိုယ်ပိုင် error ကို raise လုပ်နိုင်ဖို့
  • List ထဲက position က ဘာကြောင့် မကောင်းတဲ့ identifier ဖြစ်လဲ (negative index ထောင်ချောက်) နဲ့ id က ဘာကြောင့် တည်ငြိမ်လဲ ရှင်းပြနိုင်ဖို့
  • split နဲ့ case/when ကိုသုံးပြီး command တွေကို parse လုပ်ကာ Integer(text, 10, exception: false) နဲ့ user ရိုက်တဲ့ နံပါတ်ကို ပြောင်းနိုင်ဖို့
  • Command layer က I/O ကို တိုက်ရိုက်မလုပ်ဘဲ String ပြန်ပေးလို့ script ဒါမှမဟုတ် တကယ့် terminal နှစ်မျိုးလုံးက မောင်းနိုင်စေဖို့
  • သတ်မှတ်ထားတဲ့ error တွေကို နယ်နိမိတ်တစ်နေရာတည်းမှာ rescue လုပ်ပြီး ရင်းနှီးတဲ့ message အဖြစ် ပြောင်းနိုင်ဖို့

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

ရိုးရိုးလေး ရှင်းရရင်

Part 1 က task ထည့်ပေးတာပဲ လုပ်နိုင်တဲ့ tracker ကို ပေးခဲ့ပါတယ်။ Part 2 မှာ သုံးလို့ရအောင် လုပ်ပါမယ်။ task တစ်ခုကို ရှာ၊ done လုပ်၊ ဖျက်ပြီး ဒါအားလုံးကို add၊ list၊ done 2၊ delete 1 လို စာသား command တွေနဲ့ မောင်းနှင်ပါမယ်။

အလွှာအသစ်က ပါးပါးလေး ဘာသာပြန်ဆိုသူတစ်ယောက်လိုပါ။ စာသားတစ်ကြောင်းကို ယူပြီး ဘယ် tracker method ကို ဆိုလိုတာလဲ ဆုံးဖြတ်ကာ အဖြေကို string အဖြစ် ပြန်ပေးပါတယ်။ တကယ့်စည်းမျဉ်းတွေက Part 1 က class တွေထဲမှာပဲ ကျန်ရှိနေလို့ ဘာသာပြန်သူက တိုနေပြီး၊ တစ်ယောက်ယောက်က အဓိပ္ပာယ်မရှိတာတွေ ရိုက်ထည့်လည်း crash မဖြစ်ဘဲ ယဉ်ကျေးတဲ့ message နဲ့ ပြန်ဖြေပါတယ်။

Problem → Why → Solution

ပြဿနာ: position နဲ့ ရွေးတဲ့ command

User ကို task ရွေးခိုင်းဖို့ ထင်ရှားတဲ့နည်းက list ထဲက position ပါ။ "done 1" က ပထမ task ကို complete လုပ်တာပေါ့။ သဘာဝကျပြီး Ruby code က tasks[number - 1] တစ်ကြောင်းတည်းပါ။ ပြဿနာက user ရိုက်နိုင်တဲ့ နံပါတ်တွေမှာ စပါတယ်။ Position 0 က index -1 ဖြစ်သွားပြီး Ruby မှာ negative index က နောက်ဆုံးကနေ ရေတွက်လို့ tasks[-1] က နောက်ဆုံး task ကို တိတ်တိတ်ဆိတ်ဆိတ် ပြန်ပေးပါတယ်။ "done 0" က မှားတဲ့ task ကို complete လုပ်ပြီး အောင်မြင်ကြောင်း ပြောပါတယ်။

Position တွေက ရွေ့လည်း ရွေ့ပါတယ်။ Task 1 ကို ဖျက်လိုက်ရင် ကျန်တာအားလုံး အပေါ်တက်သွားလို့ "done 3" က တစ်မိနစ်ကထက် မတူတဲ့ task ကို ဆိုလိုသွားပါတယ်။ Position တွေပါတဲ့ command log ဒါမှမဟုတ် သိမ်းထားတဲ့ file က အဓိပ္ပာယ်မရှိတော့ပါဘူး။

positions.rb
tasks = ["Buy milk", "Learn Ruby", "Write tests"]

def pick(tasks, number)
  tasks[number - 1]
end

p pick(tasks, 2)   # "Learn Ruby"
p pick(tasks, 0)   # "Write tests" - index -1 is the LAST element, no error
p pick(tasks, 7)   # nil - and nil.title would crash later
tasks.delete_at(0)
p pick(tasks, 2)   # "Write tests" - the same number now means another task

Ruby က ဘာကြောင့် မတားတာလဲ

Ruby ရဲ့ array indexing က position မှားလို့ ဘယ်တော့မှ raise မလုပ်ပါဘူး။ negative number တွေက ပတ်သွားပြီး အဆုံးကျော်တဲ့ နံပါတ်တွေက nil ပေးပါတယ်။ Loop တွေမှာ အဆင်ပြေပေမယ့် user input အတွက်တော့ အန္တရာယ်ရှိပါတယ်။ တည်ငြိမ်တဲ့ id နဲ့ ကိုက်ညီတာမရှိရင် raise လုပ်တဲ့ lookup ပေါင်းစပ်လိုက်ရင် နံပါတ်မှားတိုင်း မြင်ရတဲ့ error ဖြစ်လာပါတယ်။

Core concept: ပျက်ကွက်ရင် ထင်ရှားစွာ သိရတဲ့ lookup၊ string ပြန်တဲ့ command

Part 2 က Part 1 အပေါ်မှာ အရာနှစ်ခု ထပ်ထည့်ပါတယ်။ Task တစ်ခုချင်းကို လုပ်ဆောင်တဲ့ tracker method တွေနဲ့ အဲဒါတွေကို ခေါ်တဲ့ command layer ပါ။ နှစ်ခုစလုံးက အတွေးတူပါ။ အောင်မြင်တာကို ထင်ရှားစေပြီး ကျရင် လွတ်သွားလို့ မရအောင် လုပ်ပါ။

၁။ raise လုပ်တဲ့ fetch နဲ့ id အလိုက် ရှာပါ

TaskTracker#fetch(id) က id ကိုက်တဲ့ task ကို ရှာပေးပြီး မရှိရင် TaskNotFound ကို raise လုပ်ပါတယ်။ နာမည်ကို Hash#fetch နဲ့ Array#fetch က ယူထားတာပါ၊ key မရှိရင် သူတို့လည်း raise လုပ်ပါတယ်။ ဒီအခါ caller က nil ကို မတော်တဆ ရလာပြီး နောက်နှစ်ကြောင်းမှာ NoMethodError နဲ့ crash မဖြစ်တော့ပါဘူး။

၂။ ကိုယ်ပိုင် error class သတ်မှတ်ပါ

class TaskNotFound < StandardError က ဒီပျက်ကွက်မှုကို နာမည်ပေးလိုက်တာပါ။ Caller တွေက ဒီ class ကိုပဲ rescue လုပ်နိုင်ပြီး စာလုံးပေါင်းမှားတာနဲ့ bug တွေကို မဖမ်းမိပါဘူး။ Message ("no task with id 99") ကိုလည်း class အတွင်းမှာ တစ်ခါတည်း ဆောက်လို့ program တစ်ခုလုံးက အတူတူ အစီရင်ခံပါတယ်။

၃။ ရည်ရွယ်ချက်ပေါ်လွင်တဲ့ method တွေနဲ့ ပြောင်းပါ

complete(id) က task ကို fetch လုပ်ပြီး Task#complete! ကို ခေါ်ပါတယ်၊ complete! က @done ကို true လုပ်ပြီး self ကို ပြန်ပေးပါတယ်။ remove(id) က အရင် fetch လုပ်ပြီးမှ ဖျက်လို့ id မရှိရင် ဘာမှမပြောင်းခင် raise ဖြစ်ပါတယ်။ နှစ်ခုစလုံးက task ကို ပြန်ပေးလို့ command layer က ဖြစ်သွားတာကို print ထုတ်နိုင်ပါတယ်။

၄။ နံပါတ်တွေကို တင်းကျပ်စွာ parse လုပ်ပါ

User ရဲ့ နံပါတ်က စာသားအဖြစ် ရောက်လာပါတယ်။ "abc".to_i က 0၊ "12abc".to_i က 12 ဖြစ်ပြီး နှစ်ခုစလုံး တိတ်တိတ်ဆိတ်ဆိတ် မှားနေပါတယ်။ Integer(text, 10, exception: false) ကတော့ စာသားတစ်ခုလုံးက base-10 integer မှန်မှသာ ပြန်ပေးပြီး မဟုတ်ရင် nil ပါ။ 10 ကို ထင်ရှားစွာ ပေးထားတာကြောင့် "010" ကို octal 8 အဖြစ် မဖတ်တော့ပါဘူး။

၅။ Command layer ကနေ string ပြန်ပါ

Cli#execute(line) က line ကို command စာလုံးနဲ့ optional argument အဖြစ် ခွဲ၊ case/when နဲ့ tracker method ကို ရွေးပြီး reply ပြန်ပေးပါတယ်။ ကိုယ်တိုင် puts မခေါ်ပါဘူး။ Reply ကို ဘယ်ကို ပို့မလဲဆိုတာ caller က ဆုံးဖြတ်ပါတယ်၊ terminal၊ test ဒါမှမဟုတ် web page ပါ။

၆။ နယ်နိမိတ်မှာ တစ်ခါတည်း rescue လုပ်ပါ

execute ရဲ့ အဆုံးမှာ TaskNotFound နဲ့ ArgumentError အတွက် rescue တစ်ခုတည်း ရှိပြီး "Error: ..." reply အဖြစ် ပြောင်းပေးပါတယ်။ Model က raise လုပ်၊ နယ်နိမိတ်က ဘာသာပြန်ပါတယ်။ အတွင်းပိုင်း method တွေမှာ begin/rescue ရှုပ်ထွေးမှု မရှိပါဘူး။

ရိုက်ထည့်တာရလဒ် (အကြောင်းရင်း)
done 2Completed #2 ... (task 2 ရှိတယ်)
done 99Error: no task with id 99 (TaskNotFound)
done twoError: expected a task id (Integer() က nil ပြန်)
add (title မပါ)Error: title can't be blank (Task.new raise)
bogusUnknown command နဲ့ help စာသား
Mental model: တံခါးဝက ဘာသာပြန်သူ
  1. စာသားတစ်ကြောင်း ဖတ်
  2. Command နဲ့ argument ခွဲ
  3. Tracker method တစ်ခု ခေါ်
  4. Reply String ပြန်
  5. Error တွေကို message ပြောင်း

စာသားဝင်၊ string ထွက်။ အဲဒီနှစ်ခုကြားက အရာအားလုံးက terminal မလိုဘဲ test ထဲကနေ ခေါ်လို့ရတဲ့ ရိုးရိုး Ruby ပါ။

အခြေခံ ပုံစံ

ruby
def fetch(id) = @items.find { |item| item.id == id } || raise(NotFound, id)

def execute(line)
  command, arg = line.strip.split(/\s+/, 2)
  case command
  when "show" then show(arg)
  else "Unknown command #{command.inspect}"
  end
rescue NotFound => e
  "Error: #{e.message}"
end

fetch က ကိုက်ညီတာမရှိရင် raise လုပ်ပါတယ်။ execute က command ကို ခွဲပြီး method တစ်ခုကို ရွေးကာ နောက်ဆုံးမှာ rescue တစ်ခုနဲ့ reply string အဖြစ် ပြောင်းပေးပါတယ်။

Part 2 code: ရှာ၊ complete၊ ဖျက်နဲ့ command layer

ဒီ file ထဲမှာ Part 1 ရဲ့ Task နဲ့ TaskTracker က ရှိပြီး "New in Part 2" comment နဲ့ အမှတ်အသား ပြထားတဲ့ အပိုင်းတွေ ထပ်ထည့်ထားပါတယ်။ Task#complete!၊ TaskTracker ရဲ့ fetch၊ complete၊ remove၊ pending၊ TaskNotFound error အသစ်နဲ့ Cli class တို့ပါ။ Scripted session တစ်ခုကို Cli ကို line တစ်ကြောင်းချင်း ထည့်ပေးပြီး reply တစ်ခုချင်းကို အဲဒါ ဖြစ်စေတဲ့ command အောက်မှာ print ထုတ်ပါတယ်။

task_tracker.rb
# New in Part 2: a named error, so callers can rescue exactly this situation.
class TaskNotFound < StandardError
  def initialize(id)
    super("no task with id #{id}")
  end
end

class Task
  PRIORITIES = %i[low medium high].freeze

  attr_reader :id, :title, :priority

  def initialize(id:, title:, priority: :medium, done: false)
    unless id.is_a?(Integer) && id.positive?
      raise ArgumentError, "id must be a positive integer"
    end

    clean = title.to_s.strip
    raise ArgumentError, "title can't be blank" if clean.empty?
    unless PRIORITIES.include?(priority)
      allowed = PRIORITIES.inspect
      raise ArgumentError, "priority must be one of #{allowed}, got #{priority.inspect}"
    end
    raise ArgumentError, "done must be true or false" unless [true, false].include?(done)

    @id = id
    @title = clean
    @priority = priority
    @done = done
  end

  def done? = @done

  # New in Part 2: the only way a task changes after it was created.
  def complete!
    @done = true
    self
  end

  def to_h
    { "id" => id, "title" => title, "priority" => priority.to_s, "done" => done? }
  end

  def to_s
    "##{id} [#{done? ? "x" : " "}] #{title} (#{priority})"
  end
end

class TaskTracker
  include Enumerable

  def initialize
    @tasks = []
    @next_id = 1
  end

  def add(title, priority: :medium)
    task = Task.new(id: @next_id, title: title, priority: priority)
    @tasks << task
    @next_id += 1
    task
  end

  def each(&block)
    return enum_for(:each) unless block

    @tasks.each(&block)
    self
  end

  def size = @tasks.size
  def empty? = @tasks.empty?

  # New in Part 2: look up, complete and remove by id. fetch raises when nothing matches.
  def fetch(id)
    @tasks.find { |task| task.id == id } || raise(TaskNotFound, id)
  end

  def complete(id) = fetch(id).complete!

  def remove(id)
    task = fetch(id)
    @tasks.delete(task)
    task
  end

  def pending = reject(&:done?)
end

# New in Part 2: turns one line of text into a tracker call and returns a String reply.
class Cli
  HELP = "commands: add <title> [!low|!medium|!high], list, done <id>, delete <id>"

  def initialize(tracker)
    @tracker = tracker
  end

  def execute(line)
    command, arg = line.strip.split(/\s+/, 2)
    case command
    when "add"    then add(arg)
    when "list"   then list
    when "done"   then "Completed #{@tracker.complete(parse_id(arg))}"
    when "delete" then "Deleted #{@tracker.remove(parse_id(arg))}"
    when nil      then HELP
    else "Unknown command #{command.inspect}. #{HELP}"
    end
  rescue TaskNotFound, ArgumentError => e
    "Error: #{e.message}"
  end

  private

  def add(arg)
    title, flag = arg.to_s.match(/\A(.*?)(?:\s+!(\w+))?\z/).captures
    priority = flag ? flag.to_sym : :medium
    "Added #{@tracker.add(title, priority: priority)}"
  end

  def list
    return "No tasks yet." if @tracker.empty?

    lines = @tracker.map(&:to_s)
    lines << "#{@tracker.pending.size} of #{@tracker.size} pending"
    lines.join("\n")
  end

  def parse_id(text)
    id = Integer(text.to_s, 10, exception: false)
    id || raise(ArgumentError, "expected a task id, got #{text.inspect}")
  end
end

script = <<~SESSION
  add Buy milk !high
  add Learn Ruby
  add Write tests !low
  list
  done 2
  done 99
  done two
  delete 1
  add
  add Plan trip !urgent
  list
  bogus
SESSION

cli = Cli.new(TaskTracker.new)
script.each_line do |line|
  puts "> #{line.strip}"
  puts cli.execute(line)
end

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

  1. fetch raises instead of returning nil@tasks.find { |task| task.id == id } || raise(TaskNotFound, id)

    find က ကိုက်ညီတာမရှိရင် nil ပြန်ပြီး || ရဲ့ ညာဘက်က raise လုပ်ပါတယ်။ ဒါကြောင့် "done 99" က "Error: no task with id 99" ဖြစ်ပြီး task တစ်ခုမှ မပြောင်းပါဘူး။

  2. An error that builds its own messagesuper("no task with id #{id}")

    TaskNotFound.new(id) ဆိုတာနဲ့ message ကို class ကိုယ်တိုင်က ဆောက်ပါတယ်။ raise(TaskNotFound, id) က id ကို initialize ထဲ ပို့ပေးလို့ raise လုပ်တဲ့နေရာတိုင်းမှာ ဝေါဟာရက တူညီပါတယ်။

  3. Fetch first, delete secondtask = fetch(id)

    remove ရဲ့ ပထမ line က fetch ပါ။ id မရှိရင် ဒီမှာ raise ဖြစ်ပြီး @tasks.delete(task) ကို ဘယ်တော့မှ မရောက်ပါဘူး။ ဖျက်ပြီးမှ task ကို ပြန်ပေးလို့ "Deleted #1 ..." ကို print ထုတ်နိုင်ပါတယ်။

  4. Split with a limitcommand, arg = line.strip.split(/\s+/, 2)

    ဒုတိယ argument 2 က အပိုင်းနှစ်ပိုင်းထက် မပိုအောင် ကန့်သတ်ပါတယ်။ "add Buy fresh milk" က ["add", "Buy fresh milk"] ဖြစ်ပြီး title ထဲက space တွေ မပျောက်ပါဘူး။ "list" တစ်လုံးတည်းဆိုရင် arg က nil ပါ။

  5. Strict number parsingid = Integer(text.to_s, 10, exception: false)

    text.to_s ကြောင့် nil ကိုလည်း ကိုင်တွယ်နိုင်ပါတယ်။ ရလဒ်က nil ဆိုရင် နောက် line က ArgumentError ကို raise လုပ်ပြီး expected a task id, got "two" ဆိုတဲ့ message ပေါ်ပါတယ်။

  6. A priority flag at the endtitle, flag = arg.to_s.match(/\A(.*?)(?:\s+!(\w+))?\z/).captures

    (.*?) က title ကို တတ်နိုင်သမျှ တိုတိုယူပြီး optional အုပ်စု (?:\s+!(\w+))? က အဆုံးက !high ကို ဖမ်းပါတယ်။ \A နဲ့ \z က string တစ်ခုလုံးကို ကိုက်ညီစေပါတယ်။ !urgent လို priority မှားကို Task.new က ငြင်းပယ်ပါတယ်။

  7. One boundary rescuerescue TaskNotFound, ArgumentError => e

    Error နှစ်မျိုးကို "Error: ..." reply အဖြစ် ပြောင်းပါတယ်။ ArgumentError က အကျယ်ကြီးမို့ argument အရေအတွက်မှားတဲ့ ကိုယ့် bug ကိုလည်း ဖမ်းနိုင်တာကို သတိထားပါ၊ ဒါကြောင့် rescue ကို execute လို တိုတဲ့ method တစ်ခုထဲမှာပဲ ထားပါတယ်။

You should see
> add Buy milk !high
Added #1 [ ] Buy milk (high)
> add Learn Ruby
Added #2 [ ] Learn Ruby (medium)
> add Write tests !low
Added #3 [ ] Write tests (low)
> list
#1 [ ] Buy milk (high)
#2 [ ] Learn Ruby (medium)
#3 [ ] Write tests (low)
3 of 3 pending
> done 2
Completed #2 [x] Learn Ruby (medium)
> done 99
Error: no task with id 99
> done two
Error: expected a task id, got "two"
> delete 1
Deleted #1 [ ] Buy milk (high)
> add
Error: title can't be blank
> add Plan trip !urgent
Error: priority must be one of [:low, :medium, :high], got :urgent
> list
#2 [x] Learn Ruby (medium)
#3 [ ] Write tests (low)
1 of 2 pending
> bogus
Unknown command "bogus". commands: add <title> [!low|!medium|!high], list, done <id>, delete <id>
Command line တစ်ကြောင်းရဲ့ ခရီးစဉ်
  1. "done 2" ဆိုတဲ့ စာသား လက်ခံ
  2. Command နဲ့ argument ခွဲ
  3. Integer(text, 10) နဲ့ id parse
  4. tracker.complete(id) ခေါ်
  5. "Completed #2 ..." ပြန်

id ရှိ

A Completed reply is returned

id မရှိ ဒါမှမဟုတ် နံပါတ်မဟုတ်

An Error reply is returned and nothing changes

တကယ့် terminal ကနေ မောင်းနှင်ခြင်း

Cli#execute က string ပဲ ပြန်လို့ terminal ပေါ်မှာ လည်ပတ်စေတာက loop ငယ်တစ်ခုပဲ လိုပါတယ်။ အဓိကအချက်က $stdin.gets ပါ။ Input ပြီးဆုံးတဲ့အခါ (Ctrl-D ဒါမှမဟုတ် pipe ထဲက file ကုန်တဲ့အခါ) gets က nil ပြန်လို့ while (line = $stdin.gets) က ရှင်းရှင်းလင်းလင်း ရပ်ပါတယ်။ gets.chomp လို့ ရေးရင်တော့ အဲဒီအချိန်မှာ Ruby 4.0.9 မှာ "undefined method 'chomp' for nil (NoMethodError)" နဲ့ crash ဖြစ်ပါတယ်။

bin/tracker
# Needs Task, TaskTracker and Cli from the example above.
cli = Cli.new(TaskTracker.new)
puts Cli::HELP

while (line = $stdin.gets)
  break if line.strip == "exit"

  puts cli.execute(line)
end

Test တွေမှာ $stdin အစား StringIO ကို အစားထိုးလို့ရပါတယ်၊ StringIO က gets နဲ့ each_line ကို တူညီစွာ ဖြေပါတယ်။ အောက်က snippet က input ကုန်သွားတာကို ပြပါတယ်။ ဒီလို scripted input နဲ့ test ရေးထားရင် keyboard ကို ထိုင်ရိုက်စရာ မလိုတော့ပါဘူး။

scripted_input.rb
require "stringio"

input = StringIO.new("add Buy milk\nlist\n")

while (line = input.gets)
  puts "got: #{line.strip}"
end

puts "at the end, gets returns #{input.gets.inspect}"

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

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

ဒီလို command parser တွေက လက်တွေ့ tool တွေရဲ့ နယ်နိမိတ်မှာ ရှိပါတယ်။ "/remind 3" ကို ဖတ်တဲ့ chat bot၊ git sub-command၊ REPL၊ web app တစ်ခုရဲ့ admin console တို့ပါ။ ပုံစံက အမြဲတူပါတယ်။ line ကို ခွဲ၊ ပထမစာလုံးနဲ့ dispatch လုပ်၊ domain object ပေါ်က method ကို ခေါ်၊ ကျရင် message အဖြစ် ပြောင်းပါတယ်။ execute က print မလုပ်ဘဲ string ပြန်လို့ Cli class တစ်ခုတည်းက ဒီနေ့ terminal ကို၊ မနက်ဖြန် Slack message ဒါမှမဟုတ် test assertion ကို ဖြေနိုင်ပါတယ်။

Program ကြီးလာတာနဲ့အမျှ ကိုယ်ပိုင် error class တွေက ပိုအရေးကြီးလာပါတယ်။ Web app တစ်ခုက NotFound class ကို rescue လုပ်ပြီး 404 စာမျက်နှာ ပြ၊ validation class ကို rescue လုပ်ပြီး message ပါတဲ့ form ပြ၊ ကျန်တာအားလုံးကို monitoring tool ဆီ ပေးလိုက်ပါတယ်။ TaskNotFound က string ပါတဲ့ RuntimeError တစ်ခုတည်း ဖြစ်နေရင် ဒီလမ်းကြောင်းခွဲတာ မဖြစ်နိုင်ပါဘူး။ Position အစား id သုံးတာလည်း သင်ခန်းစာတူပါ။ /tasks/42 လို web URL တွေက တခြား row တွေ ဖျက်ခံရလည်း ဘာကြောင့် တည်ငြိမ်နေတာလဲဆိုတာ အဲဒီအကြောင်းပါပဲ။

အမှားများ

ဖြစ်လေ့ရှိတဲ့ အမှားများ

to_i ကို id ခန့်မှန်းခိုင်းတာ

"two".to_i က 0 ဖြစ်ပြီး position math နဲ့ဆိုရင် tasks[-1] ဖြစ်ကာ နောက်ဆုံး task ကို complete လုပ်ပစ်ပါတယ်။ id နဲ့တောင် "12abc".to_i က 12 ဖြစ်လို့ user က အဓိပ္ပာယ်မရှိတာ ရိုက်ထားပေမယ့် task 12 ကို complete လုပ်ပါလိမ့်မယ်။ Integer(text, 10, exception: false) ကို သုံးပြီး nil ကို error အဖြစ် သတ်မှတ်ပါ။

နေရာတိုင်းမှာ StandardError ကို rescue လုပ်တာ

execute ရဲ့ထိပ်မှာ rescue => e လို့ ရေးရင် ကိုယ့် typo ကြောင့်ဖြစ်တဲ့ NoMethodError ကိုပါ မျိုချပြီး "Error: undefined method" လို ယဉ်ကျေးတဲ့ reply အဖြစ် ပြောင်းလို့ bug ကို ဖုံးထားမိပါတယ်။ မျှော်လင့်ထားတဲ့ class တွေဖြစ်တဲ့ TaskNotFound နဲ့ ArgumentError ကိုပဲ rescue လုပ်ပါ။

ဖျက်တဲ့ method က missing ကို မပြောဘူးလို့ ယုံတာ

@tasks.delete_at(99) က ဘာ error မှ မပေးဘဲ nil ပြန်ပါတယ်။ @tasks.delete(task) ကလည်း မတွေ့ရင် nil ပါ။ အဲဒီ nil ကို "Deleted #{task.title}" ထဲ ထည့်လိုက်ရင် နောက်တစ်ကြောင်းမှာမှ NoMethodError ဖြစ်ပါတယ်။ ဖျက်မယ့် task ကို fetch နဲ့ အရင်ရှာပြီးမှ ဖျက်ပါ။

Model ထဲမှာ ရိုက်ထုတ်တာ

complete ထဲမှာ puts "Completed" လို့ ရေးရင် test မှာ တိတ်တိတ်ဆိတ်ဆိတ် မခေါ်နိုင်သလို တခြား interface အတွက်လည်း ပြန်မသုံးနိုင်ပါဘူး။ Task ကို ပြန်ပေးပြီး format လုပ်တာကို caller ကို ထားပါ။

Best Practices

Best practices

  • Error ကို ယန္တရားအမည်မဟုတ်ဘဲ အခြေအနေအမည်ပေးပါ (TaskNotFound)
  • Error message ကို error class အတွင်းမှာ ထားပါ၊ ဒါမှ raise လုပ်တဲ့နေရာတိုင်း တူညီပါမယ်
  • Rescue ကို နယ်နိမိတ်တစ်နေရာတည်းမှာ ထားပြီး အတွင်းပိုင်း method တွေကို သန့်ရှင်းအောင် ထားပါ
  • Model ကို မခေါ်ခင် နယ်နိမိတ်မှာပဲ စာသားကို Integer၊ Symbol၊ Date အဖြစ် တစ်ခါတည်း parse လုပ်ပါ

Mini challenge

Task အများကို တစ်ခါတည်း complete လုပ်ပါ

done 1 3 5-7 လို command တစ်ခုက task အများကို ပြောနိုင်အောင် parse_ids(text) ကို ရေးပါ။ Array နှစ်ခု ပြန်ပါမယ်။ လုပ်ဆောင်ရမယ့် id တွေနဲ့ နားမလည်လို့ မရတဲ့ token တွေပါ။

  • Whitespace နဲ့ ခွဲပြီး နံပါတ်ရိုးရိုးနဲ့ 5-7 လို inclusive range တွေကို လက်ခံပါ
  • Id တွေကို ပေးထားတဲ့ အစီအစဉ်အတိုင်း ထားပြီး ထပ်နေတာတွေကို ဖယ်ပါ
  • ပထမတစ်ခုမှာ မရပ်ဘဲ မှားတဲ့ token အားလုံးကို report လုပ်ပါ (စာလုံး၊ 0 ဒါမှမဟုတ် အနုတ်နံပါတ်၊ 7-5 လို ပြောင်းပြန် range၊ id 1000 ထက် ကျယ်တဲ့ range)
  • ဗလာ ဒါမှမဟုတ် nil စာသားက Array အလွတ်နှစ်ခု ပြန်ရပါမယ်
parse_ids.rb
def parse_ids(text)
  ids = []
  rejected = []

  # TODO: split the text into tokens (nil must behave like "")
  # TODO: a token is either "7" or "5-7" - anything else goes into rejected
  # TODO: a range expands to every id from first to last; first > last is rejected
  # TODO: keep ids in order without duplicates

  [ids, rejected]
end

ရလဒ်: Input ခုနစ်ခုစလုံးအတွက် ids=[...] နဲ့ rejected=[...] line တစ်ကြောင်းစီ ထွက်ပါမယ်။ "1 3 5-7" က [1, 3, 5, 6, 7]၊ "4 x 0 7-5 -1" က ids [4] နဲ့ rejected တွင် "x"၊ "0"၊ "7-5"၊ "-1"၊ ဗလာနဲ့ nil က နှစ်ခုစလုံး အလွတ်ပါ။

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

Token တစ်ခုချင်းကို /\A(\d+)(?:-(\d+))?\z/ လို regular expression နဲ့ စစ်ပါ။ ဒုတိယ capture မပါရင် range မဟုတ်ဘဲ နံပါတ်တစ်ခုတည်းမို့ last = first ထားပါ။ (first..last).to_a က range ကို ချဲ့ပေးပါတယ်၊ ပြီးရင် uniq နဲ့ ထပ်နေတာတွေကို ဖယ်ပါ။

အဖြေ ကြည့်ရန်
parse_ids.rb
# Turns "1 3 5-7" into the ids to act on, and collects every token it cannot use.
def parse_ids(text)
  ids = []
  rejected = []

  text.to_s.split.each do |token|
    match = token.match(/\A(\d+)(?:-(\d+))?\z/)
    first = match && match[1].to_i
    last = match && (match[2] || match[1]).to_i

    if first.nil? || first < 1 || last < first || last - first >= 1000
      rejected << token
    else
      ids.concat((first..last).to_a)
    end
  end

  [ids.uniq, rejected]
end

["1 3 5-7", "2 2 1", "4 x 0 7-5 -1", "  9   10-11  ", "1-5000", "", nil].each do |text|
  ids, rejected = parse_ids(text)
  puts "#{text.inspect} -> ids=#{ids.inspect} rejected=#{rejected.inspect}"
end

Quick Quiz 1 / 3

User က "done 0" လို့ ရိုက်ပါတယ်။ Tracker က tasks[number - 1] နဲ့ task ကို ရှာပါတယ်။ ဘာဖြစ်မလဲ။

Quick Quiz 2 / 3

Integer("12abc", 10, exception: false) က ဘာပြန်မလဲ။

Quick Quiz 3 / 3

TaskTracker#remove က ဖျက်မယ့်အစား fetch(id) ကို အရင်ခေါ်တာ ဘာကြောင့်လဲ။

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

  • Position နဲ့ ရွေးတာက negative index နဲ့ ရွေ့သွားတဲ့ နံပါတ်တွေကြောင့် မှားတတ်လို့ တည်ငြိမ်တဲ့ id ကို သုံးပါ
  • fetch က မရှိရင် ကိုယ်ပိုင် error (TaskNotFound) ကို raise လုပ်ပြီး nil ကို တိတ်တိတ်ပြန်တာ မရှိစေပါဘူး
  • User ရဲ့ နံပါတ်ကို Integer(text, 10, exception: false) နဲ့ တင်းကျပ်စွာ parse လုပ်ပါ
  • Cli#execute က string ပြန်ပြီး နယ်နိမိတ်မှာ မျှော်လင့်ထားတဲ့ error တွေကိုပဲ rescue လုပ်ပါတယ်
  • $stdin.gets ရဲ့ nil နဲ့ StringIO က terminal နဲ့ scripted test နှစ်ခုလုံးကို မောင်းနိုင်စေပါတယ်

Official Ruby Documentation — Ruby Documentation

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

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

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

Task Tracker Project - Part 2 | Thuta Learning