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

เชี่ยวชาญ Ruby Splat Operator: สร้างและจัดการ Arrays อย่างง่ายดาย

สิ่งที่ชอบ Ruby คือความลึกของ features ใช้ operator แต่อยู่แล้วเจอว่าครียังแค่ผิวเงิน operator ตัวเล็ก * และ ** ตัวอย่างยอดเยี่ยม

คุณน่าจะใช้ splats สำหรับ "catch-all" arguments และนั่นคือสิ่งที่ส่วนใหญ่ใช้


def go(x, *args)
  puts args.inspect
end
go("a", "b", "c")

ถ้าใช้ keyword argument syntax ใหม่ ใช้ double splat:


def go(**params)
  puts params.inspect
end
go(x: 100, y: 200)

มีประโยชน์แต่ splats ทำได้มากกว่านี้ มาลุย!

ใช้ Array ส่ง Multiple Arguments

ไม่ได้แค่ define methods splats ใช้ตอน call methods ได้ด้วย ให้ส่ง array เข้า function ที่คาดหวัง multiple arguments item แรกกลาย argument แรก item ที่สอง argument ที่สอง ฯลฯ


def go(x, y)
end
point = [12, 10]
go(*point)

別忘記 double splat สำหรับ keyword arguments ใหม่:


def go(x:, y:)
end
point = { x: 100, y: 200 }
go(**point)

Splat ไม่จำเป็นต้องอยู่ท้าย

ปกติ splatted arguments วางท้าย argument list แต่ไม่มีกฎกำหนด

วาง splat ที่ไหนใน argument list ก็ได้:


def go(x, *args, y)
  puts x # => 1
  puts y # => 5
  puts args.inspect # => [2,3,4]
end
go(1, 2, 3, 4, 5)

Array Destructuring

ทุก tricks กับ arguments คือ special case ของ array destructuring

ถ้าไม่คุ้น "array destructuring" คือแยก array เป็น items ตัวต่อตัว เป็นแบบนี้:


a, b = [1, 2]
puts a # 1
puts b # 2

ทำงานดีแต่มาจำกัดต้องระบุ variable ทุก item splat operator ช่วย - เป็น wildcard มาดูตัวอย่าง

Pop first item จาก array

บางครั้งใช้ pop first item จาก array โดยไม่ alter original array ตัวอย่างนี้ทำได้:


first, *remainder = [1, 2, 3, 4, 5]
first # => 1
remainder # => [2, 3, 4, 5]

ถ้าอยากแค่ first item ไม่เอา rest ใช้ syntax:


first, * = [1, 2, 3, 4, 5]
first # => 1

Pop last item

ดึง item ท้าย array แทนหน้า ใส่ splat หน้า:


*prefix, last = [1, 2, 3, 4, 5]
last # => 5
prefix # => [1, 2, 3, 4]

อีกครั้ง ไม่อยาก variable เฉพาะ ไม่ต้อง assign:

Get first และ last n items ของ array

วาง splat operator กลาง ดึง items จำนวน arbitrary จากแต่ละข้าง:


first, *, last = [1, 2, 3, 4, 5]
first # => 1
last # => 5

Limitations

ใช้ splat operator ใน array destructuring ยังต้องระบุ position ของ array items เทียบต้น/ปลาย array ไม่ใช่ tool ดีสำหรับ extract items จากกลาง long array

และหา cool tricks ด้วย double-splat ** กับ hashes ไม่ได้ แร้ด!

Constructing arrays

Splat operator useful ไม่ได้แค่ destructuring แต่ constructing arrays ด้วย

ตัวอย่าง用 splat join สอง arrays:


[*[1,2], *[3,4]] # => [1, 2, 3, 4]

เทียบเท่า [[1, 2], [3,4]].flatten

ถ้าจบที่นี่ไม่ค่อย useful แต่ splat มีความสามารถแปลกอีก: coerce objects เข้า arrays intelligently


# ปกติ *thing wrap thing ใน array
x = *"hi mom" # => ["hi mom"]
# ...unless มัน nil
x = *nil # => []
# Arrays ผ่าน unchanged
x = *[1,2,3] # => [1, 2, 3]
# แต่ hashes แปลงเป็น arrays
x = *{a: 1} # => [[:a, 1]]

ให้ tool สร้าง arrays โดยไม่ต้อง manual type coercion มาก

เช่น collecting array of strings สำหรับ configuration system ปกติ:

  • Check array exists, initialize ถ้าไม่
  • Respond intelligently ถ้าพยายามเพิ่ม array of strings ไม่ใช่ single string

Splat operator ให้ฟรี:


# Config hash อาจมี/ไม่มี :ignore array
config = { }

# Callback อาจคืน array หรือ single item
def add_ignores
  "scoundrels" # could also be array like ["scoundrels", "cads", "ne'er-do-wells"]
end

# นี่คือ magic ไม่ว่าจะเริ่มอะไรได้ array of things to ignore
config[:ignore] = [*config[:ignore], *add_ignores()]

That's it

คาดว่า obvious ว่าอย่า rewrite array-manipulation code ทั้งหมด用 splats มันไม่ใช่ right tool เสมอ และอาจทำ code ยากอ่านถ้าใช้บ่อยเกินไป

แต่บางครั้ง array splat trick คือสิ่งที่ต้องการพอดี หวังตอนนั้นคุณลอง用ดู :)