Thuta Learning
Ruby
BasicProgrammingအခြေခံ

Get Started (Setup)

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

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

  • ကိုယ့် operating system အတွက် install နည်းလမ်း ရွေးနိုင်ဖို့: Windows မှာ RubyInstaller၊ macOS နဲ့ Linux မှာ rbenv လို version manager
  • ruby -v နဲ့ gem -v ကို သုံးပြီး installation ကို စစ်နိုင်ဖို့၊ command မတွေ့ရင် PATH ပြဿနာလို့ သိဖို့
  • hello.rb ကို ဖန်တီးပြီး file ရှိတဲ့ folder ထဲကနေ ruby hello.rb နဲ့ run နိုင်ဖို့
  • စမ်းသပ်ချက် တစ်ကြောင်းစာအတွက် irb နဲ့ ruby -e ကို သုံးပြီး syntax စစ်ဖို့ ruby -c ကို သုံးနိုင်ဖို့

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

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

Ruby interpreter လို့ခေါ်တဲ့ program က ဖတ်ပေးမှသာ Ruby code က တစ်ခုခု လုပ်ပါတယ်။ ဒါကြောင့် Ruby ကို setup လုပ်တယ်ဆိုတာ အဲဒီ interpreter ကို install လုပ်တာ၊ code ရေးဖို့ text editor ရွေးတာ၊ ကိုယ့် file ပေါ်မှာ interpreter ကို စတင်ခိုင်းဖို့ terminal ကို သုံးတတ်တာ ဆိုတဲ့ အပိုင်းသုံးပိုင်းပါ။ ရေနွေးအိုးတစ်လုံးလို မြင်ကြည့်ပါ: အိုး (interpreter) ကို ပလပ်ထိုးထားရမယ်၊ ဘယ်ခလုတ် (terminal command) က ဖွင့်ပေးလဲ သိရမယ်။ ruby -v ကတော့ ပလပ်ထိုးထားလား စစ်တဲ့ command ပါ: version နံပါတ်တစ်ခု ပေါ်လာရင် ကျန်တာအားလုံး အလုပ်လုပ်ပါလိမ့်မယ်။

Problem → Why → Solution

ပြဿနာ: "command not found" က ဘာကို ဆိုလိုတာလဲ

Ruby ကို install လုပ်ပြီး terminal ဖွင့်ကာ ruby -v လို့ ရိုက်ရင် shell က command not found လို့ ဖြေပါတယ်။ ဒါမှမဟုတ် Ruby စတင်ပေမယ့် ကိုယ့် file အကြောင်း ညည်းပါတယ်။ ပြဿနာ နှစ်မျိုး ကွဲပြားနေပေမယ့် beginner အတွက် message နှစ်ခုက တူနေသလို ထင်ရပါတယ်။ ပထမ error က terminal က ruby program ကို လုံးဝ မတွေ့ဘူးလို့ ဆိုလိုပြီး ဒုတိယက ruby ကောင်းကောင်း run ပေမယ့် ကိုယ်ရောက်နေတဲ့ folder ထဲမှာ အဲဒီနာမည်နဲ့ file မရှိဘူးလို့ ဆိုလိုပါတယ်။

မှားတဲ့ဟာကို ပြင်နေရင် ညတစ်ညလုံး ကုန်သွားနိုင်ပါတယ်: file က folder မှားနေတာကို Ruby ကို ပြန် install လုပ်လို့ မပြောင်းလဲပါဘူး။ Windows cmd မှာတော့ ပထမ message က 'ruby' is not recognized as an internal or external command ပုံစံနဲ့ ပေါ်ပါတယ်။

terminal
$ ruby -v
bash: ruby: command not found

$ ruby hello.rb
ruby: No such file or directory -- hello.rb (LoadError)

ဘယ် program က ညည်းနေလဲ ဖတ်ပါ

ပထမ message မှာ shell က ညည်းတာ (bash: ruby: ...) ဖြစ်ပြီး ဒုတိယမှာ Ruby က ညည်းတာ (ruby: No such file...) ပါ။ Colon ရှေ့က နာမည်က ဘယ်သူ ပြောနေလဲ ပြောပြပြီး installation ကို ပြင်ရမလား file လမ်းကြောင်းကို ပြင်ရမလား ဆုံးဖြတ်ပေးပါတယ်။ Windows မှာတော့ colon ရှေ့က နာမည်က ruby.exe ရဲ့ လမ်းကြောင်းအပြည့်ဖြစ်နိုင်ပါတယ်၊ အယူအဆက အတူတူပါ။

အဓိက သဘောတရား: interpreter၊ editor၊ terminal နဲ့ PATH

Ruby ကို setup လုပ်တယ်ဆိုတာ အရာသုံးခု အတူတကွ အလုပ်လုပ်အောင် လုပ်တာပါ: interpreter၊ text editor နဲ့ terminal။ Interpreter က ruby လို့ခေါ်တဲ့ program ပါ၊ editor က .rb extension နဲ့ plain text သိမ်းလို့ရတဲ့ နေရာတိုင်း (ဥပမာ VS Code) ဖြစ်ပြီး terminal က ruby hello.rb လို့ ရိုက်တဲ့ နေရာပါ။ သုံးခုကို ချိတ်ပေးတာက PATH ပါ: command နာမည်တစ်ခု ရိုက်တိုင်း terminal က ရှာကြည့်တဲ့ folder စာရင်းပါ။ Installer တွေက Ruby ရဲ့ bin folder ကို PATH ထဲ ထည့်ပေးပေမယ့် install လုပ်တုန်းက ဖွင့်ထားပြီးသား terminal က အပြောင်းအလဲကို မမြင်ပါဘူး၊ ဒါကြောင့် "command not found" အတွက် ပထမဆုံး ပြင်နည်းက terminal အသစ်ဖွင့်တာပါ။

ဘယ် Ruby ကို install လုပ်လဲဆိုတာ beginner တွေ ထင်ထားတာထက် ပိုအရေးကြီးပါတယ်။ macOS မှာ ကိုယ်ပိုင် Ruby ပါပြီးသားဖြစ်ပြီး Linux distribution တွေကလည်း package အဖြစ် ပေးထားပါတယ်၊ ဒါပေမယ့် အဲဒီ Ruby က operating system ပိုင်တာ၊ ဟောင်းနေနိုင်တာနဲ့ ပြောင်းဖို့ administrator အခွင့်အရေး လိုတာတွေ ရှိပါတယ်။ rbenv (ruby-build နဲ့) ဒါမှမဟုတ် RVM လို version manager က Ruby ကို ကိုယ့် home folder ထဲမှာ install လုပ်ပေးပြီး version များစွာကို တစ်ပြိုင်နက် ထားကာ project အလိုက် ပြောင်းလို့ရစေပါတယ်။ Windows မှာတော့ ပုံမှန်လမ်းကြောင်းက RubyInstaller ဖြစ်ပြီး PATH ကို ကိုယ့်အစား သတ်မှတ်ပေးပါတယ်၊ WSL2 (Windows ထဲက Linux ပတ်ဝန်းကျင်) ကလည်း လူကြိုက်များတဲ့ နည်းလမ်းဖြစ်ပြီး အဲဒီမှာ Linux အကြံတွေ သက်ဆိုင်ပါတယ်။

OSအသုံးများတဲ့ လမ်းကြောင်းသိထားသင့်တာ
WindowsRubyInstallerRuby ကို PATH ထဲ ထည့်ပေးပါတယ်၊ Devkit ပါတဲ့ ပုံစံက နောက်ပိုင်း gem တချို့ လိုအပ်တဲ့ MSYS2 tool အဆင့်ကို အကြံပေးပါတယ်
macOSHomebrew နဲ့ install လုပ်တဲ့ rbenv လို version managerကိုယ့် project တွေအတွက် ပါပြီးသား system Ruby ကို မသုံးပါနဲ့
LinuxDistribution package ဒါမှမဟုတ် version managerPackage (ဥပမာ Debian အခြေပြုမှာ ruby-full) က မြန်ပေမယ့် version နောက်ကျနေတတ်ပါတယ်

Ruby install ပြီးရင် command သုံးခုက အားလုံးကို အတည်ပြုပေးပါတယ်။ ruby -v က version ကို print ထုတ်လို့ ruby လို့ရိုက်ရင် ဘယ် Ruby က ဖြေလဲ သိရပါတယ်။ gem -v က Ruby နဲ့အတူ ပါလာတဲ့ library installer ဖြစ်တဲ့ RubyGems ကို စစ်ပေးပါတယ်။ irb က interactive prompt ကို ဖွင့်ပေးပြီး Ruby code တစ်ကြောင်းချင်း ရိုက်ကာ ရလဒ်ကို ချက်ချင်း မြင်ရလို့ စမ်းသပ်ချက်တွေအတွက် အကောင်းဆုံး မှတ်စုစာအုပ်ပါ။ ကိုယ့် program တွေကိုတော့ file ထဲမှာ ထားပါတယ်: hello.rb ဆိုတဲ့ plain text file ကို သိမ်းပြီး အဲဒီ file ရှိတဲ့ folder ထဲကနေ နာမည်နဲ့ run ပါတယ်။

၁။ Ruby ကို install လုပ်ပါ

ကိုယ့် OS အတွက် အပေါ်က လမ်းကြောင်းတစ်ခု ရွေးပါ။ Windows မှာ installer ထဲက PATH ထည့်တဲ့ option ကို ရွေးထားပါ။

၂။ Terminal အသစ်ဖွင့်ပြီး စစ်ပါ

Install ပြီးမှ terminal အသစ်တစ်ခု ဖွင့်ပြီး ruby -v လို့ ရိုက်ပါ။ Version နံပါတ်ပေါ်ရင် Ruby ကို terminal က တွေ့ပါပြီ။

၃။ hello.rb ကို ဖန်တီးပါ

Folder တစ်ခု ဖန်တီးပြီး editor နဲ့ hello.rb ကို သိမ်းပါ။ File နာမည်မှာ space မထည့်ဘဲ lowercase နဲ့ ရေးတာက error နည်းစေပါတယ်။

၄။ File ရှိတဲ့ folder ထဲကနေ run ပါ

Terminal ကို cd နဲ့ အဲဒီ folder ထဲ ရွှေ့ပြီး ruby hello.rb လို့ ရိုက်ပါ။

မှတ်ထားရမယ့် ပုံစံ: install၊ PATH၊ run
  1. Ruby ကို install လုပ်မယ်
  2. Terminal အသစ်က PATH ထဲ ruby ကို တွေ့မယ်
  3. ruby -v က အလုပ်လုပ်ကြောင်း သက်သေပြမယ်
  4. ruby hello.rb က file ကို run မယ်

ruby မတွေ့ရင် installation/PATH ကို ပြင်ပါ၊ file မတွေ့ရင် ကိုယ်ရောက်နေတဲ့ folder ကို စစ်ပါ။

အထွေထွေ ပုံစံ: ruby command

bash
ruby -v                  # which Ruby is on the PATH?
ruby filename.rb         # run a whole file
ruby -e 'code'           # run one line without a file
ruby -c filename.rb      # check the syntax only
irb                      # interactive prompt

ruby ကို option မပါဘဲ file နာမည်နဲ့ ခေါ်ရင် file ကို run ပါတယ်။ -v က version ကို ပြပြီး -e က နောက်က code စာသားကို file မလိုဘဲ run ပေးပါတယ်၊ -c က code ကို မ run ဘဲ syntax ကိုပဲ စစ်ပါတယ်။ irb ကတော့ သီးခြား command တစ်ခုပါ။

Example: version စစ်ပါတဲ့ hello.rb

ဒါကို hello.rb အဖြစ် သိမ်းပြီး ruby hello.rb လို့ run ပါ။ နှုတ်ဆက်စာကို print ထုတ်ပြီး ဘယ် Ruby implementation က run နေလဲ ပြောကာ version က အနည်းဆုံး 3.0 ဟုတ်မဟုတ် စစ်ပါတယ်။ Version number တွေကို plain string အဖြစ် နှိုင်းယှဉ်ရင် အဖြေမှားတတ်လို့ version စစ်တာမှာ Gem::Version ကို သုံးထားပါတယ်။

hello.rb
# hello.rb
puts "Ruby is ready!"
puts "Interpreter: #{RUBY_ENGINE}"

minimum = Gem::Version.new("3.0")
current = Gem::Version.new(RUBY_VERSION)
puts "Version check passed" if current >= minimum

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

  1. The first success lineputs "Ruby is ready!"

    ဒီ line က အရင်ဆုံး ပေါ်လာရင် installation၊ PATH၊ file လမ်းကြောင်း သုံးခုလုံး မှန်တယ်လို့ သက်သေပြပါတယ်။ puts က စာသားကို newline နဲ့ print ထုတ်ပါတယ်။

  2. A built-in constantputs "Interpreter: #{RUBY_ENGINE}"

    RUBY_ENGINE က Ruby ထဲမှာ ပါပြီးသား constant ပါ (စာလုံးအကြီးရေးထားတာ constant ဖြစ်လို့ပါ)။ CRuby မှာ "ruby" ဖြစ်ပြီး JRuby မှာ "jruby" ဖြစ်ပါတယ်။

  3. A version objectminimum = Gem::Version.new("3.0")

    Gem::Version က version နံပါတ်တွေ ဘယ်လို စီလဲ နားလည်တဲ့ class ပါ။ :: က Gem ဆိုတဲ့ namespace ထဲက Version ကို ညွှန်းပါတယ်။ minimum က နှိုင်းယှဉ်လို့ရတဲ့ object တစ်ခု ဖြစ်သွားပါတယ်။

  4. The running versioncurrent = Gem::Version.new(RUBY_VERSION)

    RUBY_VERSION က "4.0.9" လို String တစ်ခုပါ။ ဒါကို Gem::Version အဖြစ် ပြောင်းလိုက်တော့ minimum နဲ့ တူညီတဲ့ အမျိုးအစားနှစ်ခု ဖြစ်ပြီး ယုံကြည်စိတ်ချစွာ နှိုင်းယှဉ်လို့ရပါတယ်။

  5. A statement modifierputs "Version check passed" if current >= minimum

    if ကို line အဆုံးမှာ ထားတာကို modifier လို့ခေါ်ပြီး condition မှန်မှသာ ရှေ့က statement ကို run ပါတယ်။ "10.0.0" >= "3.0" က false ဖြစ်ပါတယ် (string တွေကို စာလုံးတစ်လုံးချင်း နှိုင်းယှဉ်လို့ "1" က "3" ထက် ရှေ့ကျတာပါ)၊ ဒါပေမယ့် Gem::Version နဲ့ဆိုရင် true ပါ။

You should see
Ruby is ready!
Interpreter: ruby
Version check passed

Ruby ကို run နည်းသုံးမျိုး

ruby command ကို ရည်ရွယ်ချက်အလိုက် ပုံစံကွဲတွေနဲ့ သုံးပါတယ်။ ထိန်းသိမ်းထားချင်တဲ့ program တွေအတွက် file ကို သုံးပြီး အမြန် တွက်ချက်ချင်ရင်တော့ -e ကို သုံးပါတယ်။ ကိုယ်တိုင် စမ်းကြည့်ချင်တဲ့အခါ irb ဟာ ပိုကောင်းပါတယ်၊ ရိုက်လိုက်တဲ့ expression တိုင်းရဲ့ ရလဒ်ကို => နဲ့ ပြပေးလို့ပါ။ Windows မှာ -e ရဲ့ code ကို double quote နဲ့ ဝန်းပြီး အထဲမှာ single quote သုံးပါ: ruby -e "puts 'hi'"။

Commandဘယ်အချိန် သုံးလဲ
ruby file.rbသိမ်းထားပြီး ပြန် run ချင်တဲ့ program တွေ
ruby -e '...'file မဖန်တီးဘဲ တွက်ချက်မှု ဒါမှမဟုတ် စစ်ဆေးမှု တစ်ခု
irbစိတ်ကူးတွေကို တစ်ကြောင်းချင်း စမ်းပြီး ရလဒ်တိုင်းကို မြင်ချင်တဲ့အခါ
ruby -c file.rbဘာမှ မ run ဘဲ syntax ကို စစ်တဲ့အခါ
irb session
irb(main):001> 6 * 7
=> 42
irb(main):002> "ruby".upcase
=> "RUBY"
irb(main):003> exit

အပေါ်က session က ပုံမှန်ပုံစံပါ၊ ကိုယ့် irb version ပေါ်မူတည်ပြီး prompt က နည်းနည်း ကွာနိုင်ပါတယ်။ exit ကို ရိုက်ရင် irb ကနေ ပြန်ထွက်ပါတယ်။

"command not found" ကို ပြင်နည်း

၁။ Terminal အသစ်ဖွင့်ပါ

Installer တွေက PATH ကို နောက်မှ ဖွင့်တဲ့ terminal တွေအတွက်ပဲ ပြောင်းပေးပါတယ်။ ပြီးခဲ့တဲ့ terminal အဟောင်းကို ပိတ်ပြီး အသစ် ဖွင့်ပါ။

၂။ Shell ဘာမြင်လဲ စစ်ပါ

macOS နဲ့ Linux မှာ which ruby၊ Windows မှာ where ruby လို့ ရိုက်ပါ။ လမ်းကြောင်းတစ်ခု ပြရင် ဘယ် Ruby ကို သုံးနေလဲ ပြောပြပါတယ်။ ဘာမှ မပြရင် PATH ထဲမှာ Ruby မရှိတာပါ။

၃။ Version manager ကို စစ်ပါ

rbenv သုံးရင် rbenv versions နဲ့ install လုပ်ထားတာတွေ ကြည့်ပြီး rbenv global နောက်က version နဲ့ တစ်ခု ရွေးပါ။ Shell setup ကို မ run ရသေးရင် shim တွေ အလုပ်မလုပ်ပါဘူး။

၄။ PATH option နဲ့ ပြန် install လုပ်ပါ

Windows မှာ အပေါ်က အဆင့်တွေ အလုပ်မဖြစ်ရင် RubyInstaller ကို ပြန် run ပြီး Ruby ကို PATH ထဲ ထည့်တဲ့ option ကို ရွေးထားကြောင်း သေချာပါစေ။

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

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

Team တွေက စက်ထဲမှာ ပါနေတဲ့ Ruby ကို မယုံကြည်ကြပါဘူး။ Project တစ်ခုက သူ့ရဲ့ version ကို rbenv၊ RVM၊ chruby လို tool တွေ ဖတ်တဲ့ .ruby-version file ထဲ ထားပြီး Gemfile ကလည်း တူညီတဲ့ လိုအပ်ချက်ကို ဖော်ပြနိုင်ပါတယ်၊ run နေတဲ့ Ruby မကိုက်ရင် Bundler က ဆက်မလုပ်တော့ပါဘူး။ Continuous integration server တွေနဲ့ Docker image တွေက အဲဒီ version အတိအကျကို install လုပ်လို့ program က laptop နဲ့ server တိုင်းမှာ အတူတူ အလုပ်လုပ်ပါတယ်။ Docker Hub ပေါ်က official ruby image တွေကို ဒါကြောင့် version အလိုက် tag တပ်ထားတာပါ။

Team တစ်ခုကို ဝင်တဲ့အခါ ပထမဆုံး အလုပ်က ဒီ lesson နဲ့ အတူတူပါပဲ: သတ်မှတ်ထားတဲ့ Ruby ကို install လုပ်၊ ruby -v နဲ့ အတည်ပြု၊ project ရဲ့ test တွေကို run ပါတယ်။ PATH ပြဿနာကို ရှာဖွေတတ်တာနဲ့ ဘယ် Ruby က ဖြေနေလဲ သိတာက အကြိမ်ကြိမ် သုံးရမယ့် ကျွမ်းကျင်မှုပါ၊ စက်တစ်လုံးမှာ Ruby နှစ်ခု — ဟောင်းတဲ့ system Ruby နဲ့ ကိုယ်တိုင် install လုပ်တဲ့ အသစ် — ရှိနေတတ်ပြီး PATH မှာ ပထမ ရှိတာက နိုင်ပါတယ်။

အမှားများ

အစပိုင်းမှာ မကြာခဏ ဖြစ်တတ်တဲ့ အမှားများ

၁။ Install မလုပ်ခင် ဖွင့်ထားတဲ့ terminal ကို ဆက်သုံးတာ

Installer ပြီးတာနဲ့ ရှေ့မှာ ရှိနေတဲ့ terminal ကို ဆက်သုံးရင် ruby က command not found ပေါ်ပါတယ်၊ အဲဒီ terminal ရဲ့ PATH က install မလုပ်ခင်ကပါ။ Terminal ကို ပိတ်ပြီး အသစ်ဖွင့်တာက reinstall လုပ်တာထက် မြန်ပါတယ်။

၂။ Folder မှားနေတာ ဒါမှမဟုတ် .txt ဝှက်ထားတာ

ruby hello.rb က လက်ရှိ folder ထဲက hello.rb ကိုပဲ ရှာပါတယ်။ pwd (Windows မှာ cd) နဲ့ ls (Windows မှာ dir) ကို သုံးပြီး ကိုယ်ဘယ်မှာလဲ စစ်ပါ။ Windows မှာ extension တွေကို ဝှက်ထားရင် Notepad က hello.rb.txt အဖြစ် သိမ်းမိနိုင်ပြီး ruby က No such file or directory -- hello.rb (LoadError) လို့ ပြောပါလိမ့်မယ်။

၃။ Prompt သင်္ကေတကို ပါ ရိုက်မိတာ

Tutorial တွေက prompt ကို ပြဖို့ $ ruby -v လို့ ရေးကြပါတယ်။ $ ကို ကိုယ်တိုင် ထည့်ရိုက်ရင် shell က $ ဆိုတဲ့ command နာမည်ကို run ဖို့ ကြိုးစားပြီး မအောင်မြင်ပါဘူး။ Dollar sign နောက်က စာသားကိုပဲ ရိုက်ပါ။

Best Practices

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

  • ကိုယ်ပိုင် project တွေအတွက် system Ruby ကို မသုံးဘဲ rbenv လို version manager နဲ့ Ruby ကို install လုပ်ပါ၊ administrator အခွင့်အရေးနဲ့ gem တွေ ထည့်စရာ မလိုတော့ပါဘူး။
  • File နာမည်ကို lowercase နဲ့ snake_case ရေးပြီး space မထည့်ပါနဲ့ — hello.rb၊ price_calculator.rb လိုပါ။
  • Run မလုပ်ခင် ပြဿနာ သံသယရှိရင် ruby -c file.rb နဲ့ syntax ကို အရင်စစ်ပါ၊ ဘာမှ မပြောင်းလဲဘဲ အမြန် အဖြေရပါတယ်။

Mini challenge

Mini challenge: Ruby doctor script

ကိုယ့်စက်ရဲ့ Ruby က လိုအပ်တဲ့ version ကို မီမမီ ပြောပြတဲ့ doctor.rb ကို ရေးပါ။ version နှစ်ခုအတွက် စစ်ပြီး ရလဒ်ကို yes ဒါမှမဟုတ် no အဖြစ် print ထုတ်ပါ။

  • at_least?(minimum) ကို သတ်မှတ်ပြီး RUBY_VERSION နဲ့ minimum ကနေ ဖန်တီးတဲ့ Gem::Version နှစ်ခုကို နှိုင်းယှဉ်ကာ true ဒါမှမဟုတ် false ပြန်ပါ
  • answer(flag) ကို သတ်မှတ်ပြီး true ကို "yes"၊ false ကို "no" အဖြစ် ပြောင်းပေးပါ
  • "Ruby 2.7 or newer: yes" နဲ့ "Ruby 99.0 or newer: no" ကို method နှစ်ခုလုံး သုံးပြီး print ထုတ်ပါ
  • version ကို plain string အဖြစ် နှိုင်းယှဉ်တာ မလုပ်ပါနဲ့ — ဘာကြောင့်လဲဆိုတာ ရှင်းပြနိုင်ရမယ်
doctor.rb
# doctor.rb
def at_least?(minimum)
  # TODO: compare Gem::Version objects, not plain strings
end

def answer(flag)
  # TODO: return "yes" for true and "no" for false
end

# TODO: print the result for "2.7" and for "99.0"

ရလဒ်: Program က line နှစ်ကြောင်း print ထုတ်ပါမယ် (ကိုယ့် Ruby က 2.7 နဲ့ 99.0 ကြားမှာ ရှိရင်): Ruby 2.7 or newer: yes Ruby 99.0 or newer: no

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

Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(minimum) က true ဒါမှမဟုတ် false ကို ပြန်ပေးပြီးသားပါ။ စာသားကို interpolation နဲ့ ဆောက်ပါ: "Ruby 2.7 or newer: #{answer(at_least?("2.7"))}"။ #{} ထဲမှာ double quote ထပ်သုံးတာ Ruby မှာ အဆင်ပြေပါတယ်။

အဖြေ ကြည့်ရန်
doctor.rb
def at_least?(minimum)
  Gem::Version.new(RUBY_VERSION) >= Gem::Version.new(minimum)
end

def answer(flag)
  flag ? "yes" : "no"
end

puts "Ruby 2.7 or newer: #{answer(at_least?("2.7"))}"
puts "Ruby 99.0 or newer: #{answer(at_least?("99.0"))}"

Quick Quiz 1 / 3

Ruby install လုပ်ပြီးပြီးချင်း ruby -v က command not found ပေးတယ်။ အဖြစ်နိုင်ဆုံး အကြောင်းရင်းက ဘာလဲ။

Quick Quiz 2 / 3

ruby -c hello.rb က ဘာလုပ်ပေးလဲ။

Quick Quiz 3 / 3

puts "10.0.0" >= "3.0" က ဘာ print ထုတ်မလဲ၊ ဘာကြောင့်လဲ။

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

  • Ruby setup မှာ interpreter၊ editor၊ terminal သုံးခု လိုပြီး PATH က terminal ကို ruby ဆီ ပို့ပေးပါတယ်
  • ruby -v က ဘယ် Ruby ဖြေလဲ ပြောပြပြီး ruby file.rb က file ကို run၊ ruby -c က syntax ကိုပဲ စစ်၊ irb က စမ်းသပ်ဖို့ပါ
  • command not found ဆိုရင် terminal အသစ်ဖွင့်ပါ၊ No such file ဆိုရင် ကိုယ်ရောက်နေတဲ့ folder နဲ့ file နာမည်ကို စစ်ပါ
  • Version နံပါတ်တွေကို Gem::Version နဲ့ နှိုင်းယှဉ်ပါ၊ plain string နဲ့ဆိုရင် "10.0.0" >= "3.0" က false ပါ

Ruby 4.0 Release — Ruby

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

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

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

Get Started (Setup) | Thuta Learning