Ruby Set คืออะไร?
Set เป็นคลาสที่เก็บ items เหมือน array…
แตมีลักษณะพิเศษทำให้ เร็วกว่า 10 เท่า ในสถานการณ์เฉพาะ!
นอกจากนั้น:
Items ทั้งหมดใน set รับประกันว่า unique
ใน Ruby tutorial นี้ คุณจะเรียนรู้:
- วิธีและเมื่อใช้ set สำหรับประโยชน์สูงสุด
- ความแตกต่างระหว่าง sets และ arrays!
- รายการ set methods ที่มีประโยชน์
มาเริ่มกัน!
Ruby Set Examples
Set เป็น Ruby class ช่วย สร้างรายการ unique items
ตัวอย่างว่ามีประโยชน์อย่างไร:
สมมติคุณวนรายการ products ใหญ่
แต มี duplicate entries และคุณอยากแค่ unique products
คุณใส่ใน set และ set จะ ensure product list unique โดยไม่ต้องทำงานเพิ่ม
วิธีทำ:
require 'set'
products = Set.new
products << 1
products << 1
products << 2
products # Set: {1, 2}
ประโยชน์อีกอย่าง การค้นหาในรายการนี้เร็วมาก:
products.include?(1) # true
เร็วเพราะ search ทำใน constant time
Set vs Array — เข้าใจความแตกต่าง
ตอนนี้คุณอาจ wonder…
ความแตกต่างระหว่าง set และ array คืออะไร?
Set ไม่มี direct access to elements:
products[0] # undefined method `[]'
นั่นคือความแตกต่างหลัก
แต set แปลงเป็น array ได้ทุกเมื่อ ที่ต้องการ:
products.to_a # [1, 2]
จุดประสงค์ของ set คือใช้สองลักษณะพิเศษ:
- Fast lookup times (ด้วย
include?) - Unique values
หากต้องการทั้งสอง set จะให้ performance boost ดี และไม่ต้องเรียก uniq บน array ทุกครั้งที่อยาก unique elements
Set vs Array Benchmark
Benchmark แสดงความแตกต่างประสิทธิภาพระหว่าง array และ set include?
# Ruby 2.5.0 set include: 8381985.2 i/s array include: 703305.5 i/s - 11.92x slower
เหตุผลคือ array ต้องเช็คทุก single element!
หากมี 1 million element array จะเช็ค 1 million elements ทุกครั้งที่เรียก include?
Set ไม่ต้องทำนั้น
Ruby Set Methods
มี operations/methods หลายอย่างระหว่าง sets หากมี math background อาจรู้จัก
The Union Operator
หนึ่งใน set methods ที่ useful คือ union operator:
products | (1..10) # Set: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Operator นี้รวม unique elements ของสอง sets เป็นหนึ่ง set
Btw ทำงานกับ Enumerable objects ใดๆ เช่น arrays, ranges และ hashes ไม่ใช่แค่ objects จากคลาส Set
The Difference Operator
หากอยากลบ elements ของ set หนึ่งจากอีก set นี้คือเมธอด
ตัวอย่าง:
products - (3..4) # Set: {1, 2, 5, 6, 7, 8, 9, 10}
The Intersection Operator
และนี่คือ set intersection operator:
Set.new(1..3) & Set.new(2..5) # Set: {2, 3}
ให้ elements common ในทั้งสอง sets.
Operators 3 ตัว (union, difference และ intersection) ใช้กับ arrays ได้ด้วย
Superset และ Subset
Superset คือ set ที่มี elements ทั้งหมดของอีก set
คุณเช็คได้ว่า set หนึ่งเป็น superset ของอีกอัน
แบบนี้:
Set.new(10..40) >= Set.new(20..30)
Range 10..40 contain 20..30 ภายใน
Subset คือ set ที่ประกอบจากส่วนของอีก set:
Set.new(25..27) <= Set.new(20..30)
วิธีใช้ Sorted Sets
หากอยาก set ที่ sorted ตลอดเวลา ใช้คลาส SortedSet
มี requirements บางอย่าง:
- Objects ที่เพิ่มใน set ต้อง implement
<=>method - Objects ต้อง comparable กับกัน (compare integer กับ integer, หรือ string กับ string)
ตัวอย่าง:
sorted_numbers = SortedSet.new
sorted_numbers << 5
sorted_numbers << 2
sorted_numbers << 1
sorted_numbers # SortedSet: {1, 2, 5}
Sets Video Tutorial
สรุป
คุณเรียนรู้การใช้ sets ใน Ruby สำหรับประสิทธิภาพดีกว่าและโค้ดง่ายกว่า คุณยังเรียนรู้ความแตกต่างระหว่าง arrays และ sets
กรุณาแชร์บทความนี้หากพบว่ามีประโยชน์ เพื่อให้ผู้คนอีกมากขึ้นค้นพบ 🙂
ขอบคุณที่อ่าน!