ruby - Fiber.yield using || -


i following code:

sg = fiber.new     s = 0     loop         square = s * s         s += 1         s = fiber.yield(square) || s     end end  puts sg.resume puts sg.resume puts sg.resume puts sg.resume 40 puts sg.resume puts sg.resume 0 puts sg.resume puts sg.resume 

when run, outputs:

0 1 4 1600 1681 0 1 4 

how line 6 s = fiber.yield(square) || s work? think understand component parts not line whole doing. (is there alternative way of writing might better me understand?).

(edit: code modified example page 295 'beginning ruby, novice professional 2nd ed' peter cooper.)

according docs yield

any arguments passed next resume value fiber.yield expression evaluates to.

the line

s = fiber.yield(square) || s 

assigns argument passed resume s. if value nil (or argument missing), s re-assiged s (i.e. doesn't change).

example:

sg.resume       #=> s = nil || s                 #=> s = s  sg.resume 40    #=> s = 40  || s                 #=> s = 40 

another way write is:

result = fiber.yield(square) s = result if result 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

php - How do you embed a video into a custom theme on WordPress? -