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

เชี่ยวชาญ Ruby's Loop Control: redo, retry, และ next Explained

เราพูดถึง Keyword retry ขณะ Discuss Retrying After Exceptions Little-known Counterpart redo Works Similarly แต่ Re-runs Loop Iterations แทน Whole Blocks

The redo keyword

As We Learned Previous Academy Article retry Let You Retry Piece of Code in Block:


Example นี้ Print "Iteration" ก่อน Raise Exception Rescue Block Executes Calls retry Start Block Again Beginning Result Endlessly Print Iteration redo Keyword Let You Achieve Similar Effect When Using Loops Useful Situations Need Retry While Iterating


This Will Print:

Notice Iteration Count Stays Same? Step Back Execution to Start of Loop Variant Using retry Print Exact Same Output:


You Can Use redo Implement Retrying in Loop Next Example Queue of Jobs Either Return :success or :failure Keep Re-running Same Iteration Until Job Succeeds



The next keyword

If You Want Move to Next Iteration Loop As Opposed to Move Back Start Current One Use next


This Will Print:

See Iteration Counter Keeps Incrementing? In Most Cases Using next Is What You Want Look at redo If You Need Loop Runs Exact Number of Times หรือ Needs Error Handling When Iterating Over Array

หวังว่าคุณเรียนรู้อะไรใหม่เกี่ยว Redoing Iterations in Loops และอยากรู้ความคิดของคุณเรื่อง Article นี้ (หรืออื่นๆ ใน AppSignal Academy Series) โปรดอย่าลังเล Let Us Know What You Think หรือ Ruby Subjects You'd Like to Learn More About

เชี่ยวชาญ Ruby s Loop Control: redo, retry, และ next Explained

Thijs Cadier

Chief Technology Officer & Co-Founder Thijs Sometimes Goes Missing Months End Work on Infrastructure Makes Sure Billions of Requests Handled Correctly Best Drummer of Company

บทความทั้งหมดโดย Thijs Cadier