ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
initialize ဆိုတာ Car.new က object အလွတ်တစ်ခု ဖန်တီးပြီးတာနဲ့ Ruby က အလိုအလျောက် ခေါ်တဲ့ method ပါ။ သူ့တာဝန်က object အသစ်ကို တခြားသူ မထိခင် လိုအပ်တဲ့ data ပေးဖို့ပါ။ ဘဏ်စာရင်းဖွင့်တဲ့အခါ form ဖြည့်တာနဲ့ တူပါတယ်၊ စာရင်းကို တစ်ဝက်တစ်ပျက်ဖြည့်ထားတဲ့ အနေအထားနဲ့ တည်ရှိခွင့် မပေးပါဘူး။
initialize ကို ကိုယ်တိုင် ခေါ်စရာ မလိုပါဘူး။ object အသစ်ကို ဘာတွေလိုလဲဆိုတာကို initialize ရဲ့ parameter တွေနဲ့ ဖော်ပြပြီး Car.new(...) ခေါ်တိုင်း argument တွေကို အဲဒီဆီ တိုက်ရိုက် ပို့ပေးပါတယ်။ ဒီလိုဆိုရင် brand မပါတဲ့ Car ကို ဖန်တီးလို့ မရတော့ပါဘူး၊ အမှားကိုလည်း အဲဒါကို ဖြစ်စေတဲ့ line မှာပဲ ပြောပြပါတယ်။
Problem → Why → Solution
ပြဿနာ: တစ်ဝက်တစ်ပျက် object
ရှေ့ lesson မှာ Car.new ခေါ်ပြီး setter တွေနဲ့ data ဖြည့်ခဲ့ပါတယ်။ ဘာမှ ဖြည့်ခိုင်းထားတာ မရှိလို့ object တစ်ခုက တစ်ဝက်တစ်ပျက် တည်ရှိနေနိုင်ပါတယ်။ အောက်က code မှာ color ကို ဘယ်တော့မှ မသတ်မှတ်ဘဲ description method က color ရှိမယ်လို့ ယူဆထားလို့ မေ့ထားတဲ့နေရာနဲ့ ဝေးတဲ့နေရာမှာ ကျိုးပါတယ်။
class Car
attr_accessor :brand, :color
def description
"#{color.upcase} #{brand}"
end
end
car = Car.new
car.brand = "Toyota" # the color was never set
puts car.description
# NoMethodError: undefined method 'upcase' for nilinitialize က ဘာကို အာမခံပေးလဲ
Object တစ်ခုက တစ်ဝက်တစ်ပျက် တည်ရှိနေနိုင်ရင် method တိုင်းက ဘယ် field တွေ ရှိလဲဆိုတာ ခန့်မှန်းနေရပါတယ်။ initialize က 'Car တစ်စီးမှာ brand နဲ့ color အမြဲရှိတယ်' ဆိုတာကို Ruby က အတင်းအကျပ် စစ်တဲ့ စည်းမျဉ်းတစ်ခု ဖြစ်စေပါတယ်။ argument မပြည့်စုံဘဲ Car.new ခေါ်ရင် မဆက်စပ်တဲ့ method တစ်ခုထဲမှာ နောက်မှ ကျိုးမယ့်အစား မှားတဲ့ line မှာပဲ ချက်ချင်း ကျိုးပါတယ်။
Core concept: new က ဖန်တီး၊ initialize က ဖြည့်
initialize က ထူးခြားတဲ့ အခန်းကဏ္ဍရှိတဲ့ ရိုးရိုး instance method ပါ။ Car.new က object အလွတ်တစ်ခု allocate လုပ်ပြီး new ကို ပေးခဲ့တဲ့ argument တွေနဲ့ initialize ကို ခေါ်ကာ object ကို ပြန်ပေးပါတယ်။ initialize ကို ကိုယ်တိုင် ဘယ်တော့မှ မခေါ်ပါနဲ့၊ တကယ်တော့ အဲဒါက private ဖြစ်လို့ car.initialize လို့ ခေါ်ရင် NoMethodError: private method 'initialize' called for an instance of Car ပေါ်ပါတယ်။ initialize က ဘာပြန်ပြန် လျစ်လျူရှုခံရပြီး new က object အသစ်ကိုပဲ အမြဲ ပြန်ပေးပါတယ်။
Argument တွေက new ကတစ်ဆင့် ဝင်လာတာမို့ Ruby က ရိုးရိုး method call လိုပဲ စစ်ပေးပါတယ်။ Parameter နှစ်ခုပါတဲ့ initialize ကို Car.new("Toyota") လို့ ခေါ်ရင် ArgumentError: wrong number of arguments (given 1, expected 2) ပေါ်ပါတယ်။ Keyword argument မှာ ကျန်နေရင် missing keyword: :color၊ မသိတာ ပါလာရင် unknown keyword: :colour လို့ ပြောပါတယ်။ ဒီ error တွေက လက်ဆောင်ပါ၊ တစ်ဝက်တစ်ပျက် object ကို မွေးဖွားတဲ့ အချိန်မှာတင် တားပေးလို့ပါ။
Parameter တွေမှာ default ပေးလို့ ရပါတယ် (def initialize(brand, color = "white"))၊ ဒါမှမဟုတ် keyword အဖြစ် ရေးလို့ ရပါတယ် (brand:, color: "white")။ တန်ဖိုး နှစ်ခုသုံးခုထက် များလာရင် keyword ကပိုဖတ်ကောင်းပါတယ်၊ Car.new(brand: "Toyota", color: "Red") က တန်ဖိုးတစ်ခုချင်းစီရဲ့ အဓိပ္ပာယ်ကို ပြောပြပြီး အစဉ်လည်း အရေးမကြီးတော့ပါဘူး။ စည်းမျဉ်းကောင်းတစ်ခုကတော့ ရှင်းလင်းတဲ့ တန်ဖိုး တစ်ခု နှစ်ခုအတွက် positional ကို သုံးပြီး အဲဒီထက်များရင် keyword ကို သုံးတာပါ။
initialize က validate လုပ်ဖို့နဲ့ input ကို ပုံမှန်ဖြစ်အောင် ပြင်ဖို့ နေရာလည်း ဖြစ်ပါတယ်။ raise ArgumentError, "..." နဲ့ အဓိပ္ပာယ်မရှိတာတွေကို ငြင်းပယ်ပြီး strip နဲ့ default တွေ ထည့်တာမျိုး လုပ်ရင် တခြား method တိုင်းက data ကို ယုံလို့ ရပါတယ်။ တန်ဖိုးအစက nil ဒါမှမဟုတ် array အလွတ်ဖြစ်နေရင်တောင် instance variable အားလုံးကို ဒီမှာ သတ်မှတ်ပါ၊ ဒါမှ object ရဲ့ field စာရင်းအပြည့်ကို နေရာတစ်ခုတည်းမှာ မြင်ရပြီး နာမည်မှားတာလည်း သိသာပါတယ်။ တခြား method ထဲမှာ @color အစား @colour လို ရိုက်မှားရင် error မပေးဘဲ nil ကို တိတ်တိတ်ဖတ်တာကိုလည်း သတိရပါ။
field အစုအဝေးတစ်ခုလောက်ပဲ ဖြစ်တဲ့ class တွေအတွက် Ruby က initialize ကို ရေးပေးမယ့် shortcut တွေ ပေးထားပါတယ်။ Struct.new(:brand, :color) က accessor, ==, to_s, inspect နဲ့ positional initialization ပါတဲ့ class တစ်ခု ဖန်တီးပေးပါတယ်။ Ruby 3.2 ကတည်းက ရှိတဲ့ Data.define(:brand, :color) ကတော့ ပြောင်းလို့မရတဲ့ version ပါ၊ setter မရှိဘဲ instance တွေက frozen ဖြစ်ပြီး ပြင်ထားတဲ့ copy လိုရင် with(color: "Blue") ကို သုံးပါတယ်။ နှစ်ခုလုံးက keyword argument ကိုလည်း လက်ခံပြီး Data က မှန်ကန်တဲ့ keyword အတိအကျကိုပဲ တောင်းပါတယ်။
- Car.new က object အလွတ်ကို allocate
- Ruby က argument တွေနဲ့ initialize ကို ခေါ်
- initialize က @variable တွေ assign
- new က ပြီးသွားတဲ့ object ကို ပြန်ပေး
Argument မမှန်ရင် ArgumentError က initialize မစခင်မှာတင် ပေါ်လို့ တစ်ဝက်တစ်ပျက် object ကို ဘယ်သူမှ မမြင်ရပါဘူး။
အထွေထွေ ပုံစံ
class ClassName
def initialize(required, optional = default, keyword:, other: default)
@required = required
@optional = optional
@keyword = keyword
end
end
ClassName.new(value, keyword: value)Parameter list မှာ positional, default ပါတဲ့ နဲ့ keyword (keyword: ) တွေ ရောနှောနိုင်ပါတယ်။ Body ထဲမှာ parameter တစ်ခုချင်းစီကို @ နဲ့ စတဲ့ instance variable ထဲ assign လုပ်ပါတယ်၊ ဒါမှ method ပြီးတဲ့နောက်လည်း ကျန်ပါမယ်။ ClassName.new(...) ရဲ့ argument တွေက initialize ဆီ တိုက်ရိုက် သွားပါတယ်။
Example: brand နဲ့ color ပါတဲ့ Car
ဒီ Car class က initialize ကို အသုံးပြုပြီး object တည်ဆောက်ချိန်မှာတင် brand နဲ့ color ကို သိမ်းပါတယ်။ description method က အဲဒီ instance variable တွေကို ဖတ်ပြီး စာသားတစ်ကြောင်း ဖန်တီးပါတယ်။ ရှေ့ lesson ရဲ့ setter နည်းနဲ့ မတူတာက Car.new ကနေ ထွက်လာတဲ့ object မှာ data က ပြည့်စုံပြီးသား ဖြစ်တာပါ။
Parameter က brand ပါ၊ @brand ကတော့ object ထဲမှာ သိမ်းတဲ့ instance variable ပါ။ နှစ်ခုကို ရောမထားပါနဲ့။ Parameter က initialize ပြီးတာနဲ့ ပျောက်သွားပေမယ့် @brand က object ရှိသရွေ့ ရှိနေပါတယ်။
class Car
def initialize(brand, color)
@brand = brand
@color = color
end
def description
"#{@color} #{@brand}"
end
end
toyota = Car.new("Toyota", "Red")
puts toyota.descriptionCode ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- initialize
def initialize(brand, color)new ကို ပေးတဲ့ argument နှစ်ခုကို ဒီ method က လက်ခံပါတယ်။ brand နဲ့ color က ရိုးရိုး local variable တွေပြီး initialize ပြီးတာနဲ့ ပျောက်သွားပါတယ်၊ ဒါကြောင့် နောက်တစ်ကြောင်းတွေမှာ object ထဲကို copy ကူးရပါတယ်။
- assign
@brand = brandဘယ်ဘက်က @brand က object ရဲ့ instance variable၊ ညာဘက်က brand က parameter ပါ။ ဒီ assign မပါရင် @brand က nil ပဲ ဖြစ်နေပြီး description ထဲမှာ အလွတ် ပေါ်လိမ့်မယ်။
- read
"#{@color} #{@brand}"တခြား method တွေက instance variable တွေကို ဖတ်ပါတယ်။ color ကို brand ရှေ့မှာ ရေးထားလို့ Red Toyota လို့ ထွက်ပြီး အစဉ်ပြောင်းရင် Toyota Red ဖြစ်မှာပါ။
- new
toyota = Car.new("Toyota", "Red")Argument တွေက positional ပါ၊ ပထမက brand၊ ဒုတိယက color ဖြစ်ပါတယ်။ new က object ကို allocate လုပ်ပြီး initialize("Toyota", "Red") ကို ခေါ်ကာ ပြီးသွားတဲ့ Car ကို toyota ထဲ ထည့်ပေးပါတယ်။
- output
puts toyota.descriptiondescription က @color နဲ့ @brand ကို ပေါင်းပြီး ပြန်ပေးလို့ Red Toyota လို့ ထွက်ပါတယ်။ Car.new("Toyota") လို့ argument တစ်ခုပဲ ပေးရင်တော့ description ထိ မရောက်ခင် ArgumentError ပေါ်ပါလိမ့်မယ်။
Red ToyotaDefault, keyword argument နဲ့ ArgumentError message တွေ
ပထမ code က positional parameter ကို default ပေးပြီး argument အရေအတွက် မမှန်ရင် ရတဲ့ error ကို ပြပါတယ်၊ "expected 1..2" က လက်ခံနိုင်တဲ့ အရေအတွက် အကွာအဝေးပါ။ ဒုတိယ code က keyword ပုံစံပါ၊ brand: ကို default မပေးထားလို့ မဖြစ်မနေလိုပြီး color နဲ့ doors က default ရှိပါတယ်။ အောက်က error message တွေကို Ruby 4.0.9 မှာ စမ်းပြီး ကူးထားတာပါ၊ message စာသားက version အလိုက် နည်းနည်း ကွာနိုင်ပါတယ်။
class Bike
def initialize(brand, color = "white")
@brand = brand
@color = color
end
end
begin
Bike.new
rescue ArgumentError => e
puts "ArgumentError: #{e.message}"
# wrong number of arguments (given 0, expected 1..2)
endclass Truck
def initialize(brand:, color: "white", doors: 4)
@brand = brand
@color = color
@doors = doors
end
def to_s
"#{@color} #{@brand} (#{@doors} doors)"
end
end
puts Truck.new(brand: "Toyota") # white Toyota (4 doors)
puts Truck.new(brand: "Honda", color: "blue", doors: 2) # blue Honda (2 doors)
attempts = [
-> { Truck.new }, # missing keyword: :brand
-> { Truck.new("Toyota") }, # given 1, expected 0
-> { Truck.new(brand: "Toyota", colour: "red") }, # unknown keyword: :colour
]
attempts.each do |attempt|
attempt.call
rescue ArgumentError => e
puts "ArgumentError: #{e.message}"
endဒုတိယ code ရဲ့ error သုံးခုက ဒီလို ထွက်ပါတယ်၊ missing keyword: :brand၊ wrong number of arguments (given 1, expected 0; required keyword: brand) နဲ့ unknown keyword: :colour ပါ။ ဒီထဲက တတိယတစ်ခုက colour လို စာလုံးပေါင်းမှားတာကို ချက်ချင်း ဖမ်းပေးပါတယ်၊ positional parameter တွေထက် keyword က ပိုကောင်းတဲ့ အကြောင်းရင်းတစ်ခုပါ။ lambda (-> { ... }) နဲ့ rescue ကို ဒီမှာ ကိရိယာအနေနဲ့ပဲ သုံးထားပါတယ်၊ နောက် lesson တွေမှာ အသေးစိတ် လေ့လာပါမယ်။
initialize ထဲမှာ validate လုပ်ခြင်း
initialize ကို object တစ်ခုရဲ့ ဝင်ပေါက်တံခါးလို့ မြင်ကြည့်ပါ။ အဲဒီမှာ စစ်ထားရင် Account ကို မှားတဲ့ data နဲ့ ဖန်တီးလို့ မရတော့ပါဘူး၊ ဒါကြောင့် ကျန်တဲ့ method တွေက balance က အနှုတ်မဖြစ်ဘူးလို့ ယုံနိုင်ပါတယ်။ raise ArgumentError, "..." ကို 'ဒီမှာ ရပ်ပြီး ပြဿနာကို ပြော' လို့ ဖတ်ထားပါ၊ raise နဲ့ rescue ကို Exceptions lesson မှာ အပြည့်အစုံ လေ့လာပါမယ်။ owner.strip နဲ့ ပုံမှန်ဖြစ်အောင် ပြင်တာကိုလည်း သတိထားပါ။ initialize ကို ကိုယ်တိုင်ခေါ်ကြည့်ရင် private ဖြစ်လို့ NoMethodError ပေါ်တာကိုလည်း နောက်ဆုံးမှာ ပြထားပါတယ်။
class Account
attr_reader :owner, :balance
def initialize(owner, balance = 0)
raise ArgumentError, "owner can't be blank" if owner.to_s.strip.empty?
raise ArgumentError, "balance can't be negative: #{balance}" if balance.negative?
@owner = owner.strip
@balance = balance
end
end
acct = Account.new(" Aye ", 500)
puts "#{acct.owner}: #{acct.balance}" # Aye: 500
[["", 10], ["Bo", -5]].each do |owner, balance|
Account.new(owner, balance)
rescue ArgumentError => e
puts "Rejected: #{e.message}" # owner can't be blank / ...negative: -5
end
begin
acct.initialize("X")
rescue NoMethodError => e
puts e.message # private method 'initialize' called for an instance...
endStruct နဲ့ Data.define: initialize ကို ရေးပေးတာ
| Struct | Data.define | |
|---|---|---|
| Setter | ပါတယ် (p1.x = 10) | မပါဘူး၊ ပြောင်းလို့ မရ |
| Argument ကျန်ရင် | nil နဲ့ ဖြည့်ပေး | ArgumentError (missing keyword) |
| ပြင်ထားတဲ့ copy | ကိုယ်တိုင် dup လုပ်ပြီး ပြင် | with(lat: 17.0) |
| ဘယ်အချိန် | ပြောင်းနိုင်တဲ့ field အစု | Value တစ်ခုလို အပြောင်းအလဲ မရှိတဲ့ record |
ဒီ ကွာခြားချက်တွေကို အောက်ကနေ ကိုယ်တိုင် ကြည့်ပါ။ inspect ရဲ့ ပုံစံတွေက Ruby 4.0.9 မှာ ထွက်တဲ့ ပုံစံတွေပါ၊ Struct က #<struct Point x=10, y=2> ဖြစ်ပြီး Data က #<data Coord lat=16.8, lng=96.2> ပါ။ Data.new က positional (Coord.new(16.8, 96.2)) ရော keyword ပါ လက်ခံပေမယ့် မလုံလောက်ရင် missing keyword ဆိုတဲ့ error ပေးပါတယ်။
Point = Struct.new(:x, :y)
p1 = Point.new(1, 2)
p1.x = 10
p p1 # #<struct Point x=10, y=2>
puts p1 == Point.new(10, 2) # true
p Point.new(1) # #<struct Point x=1, y=nil>
Coord = Data.define(:lat, :lng)
c1 = Coord.new(lat: 16.8, lng: 96.2)
p c1 # #<data Coord lat=16.8, lng=96.2>
p c1.with(lat: 17.0) # #<data Coord lat=17.0, lng=96.2>
puts c1.frozen? # true
begin
c1.lat = 1
rescue NoMethodError => e
puts e.message # undefined method 'lat=' for an instance of Coord
end
begin
Coord.new(lat: 1)
rescue ArgumentError => e
puts e.message # missing keyword: :lng
endလက်တွေ့ project တွေမှာ
Real project တွေမှာ ဘယ်လို သုံးလဲ
Real Ruby project တွေမှာ initialize က dependency တွေနဲ့ setting တွေ object ထဲ ဝင်တဲ့ လမ်းကြောင်းပါ။ PaymentService.new(order, gateway) လို service တစ်ခုက လိုအပ်တာအားလုံးကို initialize ကနေ လက်ခံပြီး test က တကယ့် gateway အစား အတု gateway ကို ထည့်ပေးနိုင်ပါတယ်။ Configuration object, form object နဲ့ API client တွေလည်း ဒီပုံစံပါပဲ။ Rails မှာ model တွေအတွက် initialize ကို ကိုယ်တိုင် ရေးဖို့ မလိုတာများပါတယ်၊ ActiveRecord က attribute hash လက်ခံတဲ့ initialize ပေးထားလို့ (User.new(name: "Aye")) ဖြစ်ပြီး ဝန်းကျင်က ရိုးရိုး Ruby object တွေအတွက်တော့ အမြဲရေးရပါတယ်။
initialize ကို ပေါ့ပေါ့ပါးပါး ထားပါ။ assign လုပ်၊ validate လုပ်၊ default ထည့်ပြီး ရပ်ပါ။ initialize ထဲမှာ network connection ဖွင့်တာ ဒါမှမဟုတ် file ဖတ်တာ လုပ်ရင် object ဖန်တီးရတာ နှေးပြီး test လုပ်ရတာ အဆင်မပြေလို့ အဲဒီအလုပ်ကို connect, load လို သီးသန့် method မှာ ဒါမှမဟုတ် ပထမဆုံး သုံးတဲ့အချိန်မှ လုပ်ပါ။ Coordinate, ငွေပမာဏ, API response လို ပြောင်းလဲမှု မရှိတဲ့ record သေးသေးလေးတွေအတွက်ကတော့ Data.define က မှန်ကန်တဲ့ initialize, equality နဲ့ ဖတ်လို့ကောင်းတဲ့ inspect တွေကို ဘာမှ မရေးရဘဲ ပေးပါတယ်။
အမှားများ
အဖြစ်များတဲ့ အမှားတွေ
1. Positional argument တွေကို အစဉ်ပြောင်းထည့်မိတာ
Car.new("Red", "Toyota") လို့ ရေးရင် Ruby က error မပေးဘဲ run ပြီး description က Toyota Red ကို ပြန်ပါတယ်။ နှစ်ခုလုံး string မို့ ဘာမှ မစစ်ပေးနိုင်လို့ပါ။ Parameter နှစ်ခုက အမျိုးအစားတူပြီး ရောရလွယ်ရင် keyword (brand:, color:) ကို သုံးပါ၊ ဒါမှ call site မှာတင် အဓိပ္ပာယ် ရှင်းပြီး အစဉ်မှားရင်လည်း ArgumentError ပေါ်ပါတယ်။
2. Instance variable နာမည်ကို ရိုက်မှားတာ
initialize မှာ @color ကို သတ်မှတ်ထားပြီး တခြား method မှာ @colour လို့ ဖတ်ရင် error မပေးဘဲ nil ပြန်လို့ "color=" လို့ အလွတ်နဲ့ print ပါတယ်။ Ruby က instance variable ကို မသတ်မှတ်ရသေးရင် nil ပြန်တာကြောင့်ပါ။ Instance variable အားလုံးကို initialize မှာ သတ်မှတ်ပြီး နာမည်ကို ပြန်ဖတ်စရာမလိုအောင် attr_reader နဲ့ ဖတ်ပါ၊ ဒါဆို method အမည်မှားရင် NoMethodError ပေါ်ပါလိမ့်မယ်။
3. Parameter ကို instance variable ထဲ မထည့်တာ
def initialize(brand); end လို့ ရေးထားပြီး @brand = brand မပါရင် brand က initialize ပြီးတာနဲ့ ပျောက်သွားပြီး @brand က nil ပဲ ရှိပါတယ်။ brand = brand လို့ ရေးတာလည်း မှားပါတယ်၊ local variable ကို သူ့ကိုယ်သူ assign လုပ်တာမို့ object ထဲ ဘာမှ မရောက်ပါဘူး။ Parameter တစ်ခုစီအတွက် @name = name ကို ရေးထားကြောင်း code review မှာ စစ်ပါ။
4. initialize ထဲမှာ အလုပ်ကြီး လုပ်တာ
Database ချိတ်တာ၊ file ဖတ်တာ၊ network ခေါ်တာကို initialize ထဲ ထည့်ရင် object တစ်ခုဖန်တီးတိုင်း အဲဒါတွေ ပြေးပြီး test မှာ ကာကွယ်ဖို့ ခက်ပါတယ်၊ ပြီးတော့ ကျိုးရင် Car.new ကိုယ်တိုင်က ကျိုးပါတယ်။ initialize ကို assign, validate, default သတ်မှတ်တာနဲ့ ကန့်သတ်ပြီး ကျန်တာကို သီးသန့် method တွေမှာ ထားပါ။
Best Practices
အကောင်းဆုံး အလေ့အကျင့်များ
- တန်ဖိုးတစ်ခု နှစ်ခုအတွက် positional၊ အဲဒီထက်များရင် keyword argument ကို သုံးပါ။
- initialize မှာ instance variable အားလုံးကို သတ်မှတ်ပြီး (nil ဖြစ်တောင်) object ရဲ့ field စာရင်းကို နေရာတစ်ခုတည်းမှာ ပြပါ။
- initialize ကို ပေါ့ပါးအောင် ထားပြီး မမှန်တဲ့ input ကို raise ArgumentError နဲ့ ချက်ချင်း ငြင်းပါ။
- ပြောင်းစရာမလိုတဲ့ record သေးသေးလေးတွေအတွက် ကိုယ်တိုင် initialize မရေးဘဲ Data.define ကို ဦးစားပေးပါ။
Mini challenge
Mini challenge: validate လုပ်တဲ့ Ticket
ဂီတပွဲလက်မှတ် Ticket class တစ်ခု ရေးပါ။ keyword argument တွေနဲ့ ဖန်တီးပြီး မမှန်တဲ့ တန်ဖိုးတွေကို initialize ထဲမှာတင် ငြင်းရပါမယ်။ နောက်ဆုံးမှာ Data.define နဲ့ ထိုင်ခုံ record တစ်ခုလည်း ဖန်တီးပါ။
- Ticket#initialize(event:, price:, seats: 1) ရေးပါ၊ price က အပေါင်းကိန်း မဟုတ်ရင် "price must be positive, got -5"၊ seats က 1 ကနေ 6 မဟုတ်ရင် "seats must be between 1 and 6, got 9" ဆိုပြီး ArgumentError raise လုပ်ပါ
- total method က price * seats ပြန်ပြီး to_s က "Concert x2 = 3000 yen" ပုံစံ ပြန်ရမယ်
- မှန်တဲ့ Ticket တစ်ခုကို print ထုတ်ပြီး မမှန်တဲ့ ကြိုးစားမှု သုံးခု (အနှုတ် price၊ seats 9၊ event မပါ) ကို rescue လုပ်ကာ "Rejected: <message>" ဆိုပြီး print ထုတ်ပါ
- Seat = Data.define(:row, :number) ဖန်တီးပြီး seat တစ်ခုနဲ့ number ပြောင်းထားတဲ့ with copy ကို inspect နဲ့ print ထုတ်ပါ
class Ticket
# TODO: initialize(event:, price:, seats: 1)
# raise ArgumentError for a bad price or a bad seat count
# TODO: total and to_s
end
# TODO: print one valid ticket (Concert, price 1500, 2 seats)
# TODO: three bad attempts in an array of lambdas, each rescued and printed as
# "Rejected: <message>"
# TODO: Seat = Data.define(...), a seat, and a copy made with with(...)ရလဒ်: Output က ဒီလို ထွက်ရပါမယ်: Concert x2 = 3000 yen Rejected: price must be positive, got -5 Rejected: seats must be between 1 and 6, got 9 Rejected: missing keyword: :event #<data Seat row="B", number=7> #<data Seat row="B", number=8>
အရိပ်အမြွက် ကြည့်ရန်
(1..6).cover?(seats) က seats က အကွာအဝေးထဲ ရှိမရှိ စစ်ပေးပါတယ်။ event: မပါတဲ့ call အတွက် error ကို ကိုယ်တိုင်မရေးရပါဘူး၊ keyword parameter ကို default မပေးထားရင် Ruby က missing keyword: :event ကို ကိုယ့်ဘာသာ ပစ်ပေးပါတယ်။ Data object ကို print ဖို့ puts seat.inspect ကို သုံးပါ။
အဖြေ ကြည့်ရန်
class Ticket
def initialize(event:, price:, seats: 1)
unless price.positive?
raise ArgumentError, "price must be positive, got #{price}"
end
unless (1..6).cover?(seats)
raise ArgumentError, "seats must be between 1 and 6, got #{seats}"
end
@event = event
@price = price
@seats = seats
end
def total
@price * @seats
end
def to_s
"#{@event} x#{@seats} = #{total} yen"
end
end
puts Ticket.new(event: "Concert", price: 1500, seats: 2)
attempts = [
-> { Ticket.new(event: "Concert", price: -5) },
-> { Ticket.new(event: "Concert", price: 1500, seats: 9) },
-> { Ticket.new(price: 1000) },
]
attempts.each do |attempt|
attempt.call
rescue ArgumentError => e
puts "Rejected: #{e.message}"
end
Seat = Data.define(:row, :number)
seat = Seat.new(row: "B", number: 7)
puts seat.inspect
puts seat.with(number: 8).inspectQuick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- Car.new က object ကို allocate လုပ်ပြီး argument တွေနဲ့ initialize ကို ခေါ်ကာ object အသစ်ကို ပြန်ပေးပါတယ်၊ initialize ရဲ့ return value ကို လျစ်လျူရှုပြီး initialize က private ပါ။
- Positional, default နဲ့ keyword parameter တွေ အားလုံးမှာ argument မမှန်ရင် ArgumentError (wrong number of arguments, missing keyword, unknown keyword) ပေါ်ပါတယ်။
- initialize မှာ instance variable အားလုံးကို သတ်မှတ်ပြီး raise ArgumentError နဲ့ validate လုပ်ပါ၊ ပေါ့ပါးအောင်ထားပြီး network ဒါမှမဟုတ် file အလုပ်တွေ မလုပ်ပါနဲ့။
- Struct က ပြောင်းလို့ရတဲ့ field အစုအတွက်၊ Data.define က ပြောင်းလို့မရတဲ့ record အတွက်ဖြစ်ပြီး နှစ်ခုလုံးက initialize ကို ရေးပေးပါတယ်။
Official Ruby Documentation — Ruby Documentation