DEV Community

Weerasak Chongnguluam
Weerasak Chongnguluam

Posted on • Edited on

2

ความหมายของ ~> ในการกำหนดเลข deps version ของ mix

ปกติเวลาจะเพิ่ม library เข้ามาใช้งาน เราก็จะต้องกำหนด dependencies โดยเลือกชื่อ library แล้วก็ version ที่ต้องการใช้งานในไฟล์ mix.exs

แต่ส่วนใหญ่แล้วก็จะเข้าดูใน https://hex.pm/ แล้วก็ก็อปปี้เลข version มาซึ่งมักจะเห็นแบบนี้เช่น

{:plug, "~> 1.1.0"}
Enter fullscreen mode Exit fullscreen mode

ทีนี้ไอ้ ~> มันคืออะไร แล้วมันจะเอา version ไหนบ้างให้เรา ซึ่งใน document ของ Elixir บอกเอาไว้ที่นี่ https://hexdocs.pm/elixir/Version.html ว่า

~> เนี่ยมันจะถูกแปลงเป็น >= และ < โดยช่วงที่ได้จะขึ้นอยู่กับเลขเวอร์ชันที่กำหนดให้หลัง ~> เช่น

~> Translation
~> 2.0.0 >= 2.0.0 and < 2.1.0
~> 2.1.2 >= 2.1.2 and < 2.2.0
~> 2.1.3-dev >= 2.1.3-dev and < 2.2.0
~> 2.0 >= 2.0.0 and < 3.0.0
~> 2.1 >= 2.1.0 and < 3.0.0

คือเลขเวอร์ชันมันเป็น semantic version เลขกลางคือ minor กับเลขหลังคือ patch ถ้าเรากำหนดถึง 3 หลักคือถึง patch มันก็จะเลือกเลข version >= version ที่เราใส่ให้ ~> จนไม่เกิน minor version ถัดไป

สรุปให้เข้าใจง่ายๆถ้าเราใส่ 3 หลัก มันจะอัพแค่ patch ให้เราเท่านั้นทำให้เราควบคุมให้ใช้ไม่เกิน minor version ที่เราเลือก

ถ้าใส่แค่ 2 หลัก จะได้ช่วยที่กว้างกว่า คือให้มันอัพ minor version ให้ด้วยแต่ไม่ไม่อัพ major version เช่นใส่ ~> 2.0 เป็นการควบคุมว่าถ้าสั่งอัพ จะได้ของ minor ล่าสุดแต่ major ยังคงเป็น 2 อยู่นั่นเอง

Buy Me A Coffee

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (1)

Collapse
 
veer66 profile image
Vee Satayamas

ใน gem ก็คือเหมือนกันเลยใช่เปล่าครับ ?