Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Ruby

Ruby 4.0: ฟีเจอร์สำคัญ Highlights การปล่อยตัว และคู่มืออัปเกรด

Ruby 4.0 เป็น major release เปิดตัววันที่ 25 ธันวาคม 2568 (ครบรอบ 30 ปีของ Ruby) เพื่อเฉลิมฉลอง 3 ทศวรรษของชุมชน ไม่ใช่เพราะ breaking changes ใหญ่ๆ

Ruby ไม่ได้ follow semantic versioning! Matz (ผู้สร้าง Ruby) เพิ่ม major version เมื่อ changes น่าประทับใจเขา

ข่าวดี: อัปเกรดไป Ruby 4 ควรจะไม่เจ็บปวดมาก มี features ใหม่อย่าง Ruby::Box และ ZJIT 改进 concurrency และ refinements ต่างๆ ที่ backward-compatible ส่วนใหญ่

Ruby::Box คืออะไร?

Experimental feature ที่น่าสนใจที่สุด: Ruby::Box สร้าง isolated namespaces หรือ "containers" (ไม่ใช่ Docker containers) ภายใน Ruby process

คล้าย lightweight virtualization ระดับภาษา แต่ละ Box มี state ของตัวเอง ไม่ leak definitions ไป Box อื่นหรือ main environment

Opt-in feature (ต้อง enable เอง) เพราะ performance overhead และอาจมี rough edges

Enable: RUBY_BOX=1 environment variable

ทำไม ZJIT จึงสำคัญ

Ruby 4.0 introduce ZJIT - JIT compiler ใหม่ ต่อจาก YJIT MRI ตอนนี้มี 2 JIT compilers

YJIT Recap

YJIT ("Yet Another JIT") โดย Shopify Ruby 3.1 ใช้ Lazy Basic Block Versioning compile basic blocks on-the-fly specialize ตาม runtime types เขียนใน Rust เป็น default JIT

ZJIT ต่างอย่างไร?

ZJIT ใช้ method-based JIT strategy แทนการ compile tiny blocks แบบ piecemeal ZJIT compile larger units (entire methods) ใช้ SSA (Static Single Assignment) intermediate representation และ conventional compiler pipeline

ออกแบบคล้าย "textbook" JIT compiler -> ง่ายต่อ contributors ที่จะเข้าใจและปรับปรุง

เป้าหมาย 2 ประการ: ยกระดับ performance ceiling ของ Ruby (advanced optimizations ที่ YJIT ทำไม่ได้) และทำให้ JIT hackable โดย community

Enable ZJIT ใน Ruby 4.0

ZJIT available แต่ไม่ enabled by default ต้อง build Ruby ด้วย Rust 1.85+ แล้วรันด้วย flag --zjit Default ยังคงได้ประโยชน์จาก YJIT --rjit flag ถูกลบใน release นี้

Ractors improvements ใน Ruby 4

Ruby 3.0 introduce Ractors (experimental) สำหรับ parallelism รัน multiple Ruby interpreters ใน single process เพื่อเลี่ยง GIL (Global Interpreter Lock)

Ruby 4.0: Ractors ยัง experimental แต่มี major improvements และ API changes ใกล้ mainstream usability มากขึ้น

  • แทน API เก่า ด้วย Ractor::Port mechanism - pipe/channel สำหรับ exchange values แต่ละ Ractor มี default port (Ractor.current.default_port) สร้าง custom Port objects ได้
  • Breaking changes: Ractor.yield และ Ractor#take ถูกลบ
  • Under the hood: ลด shared state ระหว่าง Ractors -> โอกาส break isolation น้อยลง CPU cache utilization ดีกว่าบน multi-core
  • ควร scale ดีกว่าและเร็วกว่า แต่ยังไม่ได้ใช้กว้างขวางเท่า threads/processes

*.nil changes ใน Ruby 4

Splatting nil ไม่เรียก nil.to_a อีกต่อไป

Ruby เก่า: arr = [*nil] เรียก nil.to_a (คืน []) -> empty array พฤติกรรม magical และ inconsistent

Ruby 4.0: *nil ไม่ invoke to_a ถือว่า "nothing to splat" สอดคล้องกับ **nil ไม่เรียก nil.to_hash (Ruby 3.4)

Logical operators เริ่มบรรทัดใน Ruby 4

Syntax improvement เล็กๆ ที่หลาย Rubyists จะชอบ!

วาง &&, ||, and, or ที่ ต้นบรรทัด เพื่อต่อ boolean expression จากบรรทัดก่อนได้แล้ว:

if user_signed_in? && user.admin? && feature_enabled?
  perform_admin_task
end

Class updates ใน Ruby 4

  • Set เป็น built-in core class แล้ว - ใช้ได้เลยไม่ต้อง require 'set' (ลบ set/sorted_set.rb)
  • Pathname promoted เป็น core class
  • Array methods ใหม่: Array#find improved performance, Array#rfind หา element สุดท้ายที่ match condition
  • Math methods ใหม่, introspection control, Enumerator improvements, และอื่นๆ

วิธีอัปเกรดไป Ruby 4

อัปเกรด production app ต้องระวัง แต่จาก Ruby 3.4 ควรเป็นอัปเกรดที่ง่ายที่สุด ที่เคยเจอ

  1. Release notes: อ่าน official release notes เช็ค deprecation notices
  2. Deprecation warnings: แก้ warnings ที่ ignore ไว้ก่อนอัปเดต
  3. Baseline tests: มี tests ให้ดี จะเชื่อมั่นได้ว่า behavior ไม่เปลี่ยน ไม่มี tests -> invest เขียน tests cover critical paths
  4. Update bundler: อัปเดต bundler -> bundle install เช็ค errors/warnings
  5. Increment Ruby version: ติดตั้งและ switch version (rbenv, asdf) Docker -> update Dockerfile
  6. Upgrade Gemfile: อัปเดต ruby version ใน Gemfile -> bundle install อีกรอบ
  7. Run tests: รัน tests! ถ้าไม่ broken -> test important paths ใช้ exception monitoring (Honeybadger) ตรวจสอบ production issues

ทำตามขั้นตอนนี้ -> อัปเกรดไม่เจ็บปวด อย่าลืมขอให้ Ruby happy 30th birthday! 🎂