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

AWS Security LIVE!

Tune in for AWS Security LIVE!

Join AWS Security LIVE! for expert insights and actionable tips to protect your organization and keep security teams prepared.

Learn More

Top comments (1)

Collapse
 
veer66 profile image
Vee Satayamas

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

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay