Can I go back to the beginning of the method using replay in Ruby?

The Poignant Guide provides an example of this redo keyword:

 class LotteryTicket def self.new_random new(rand(25) + 1, rand(25) + 1, rand(25) + 1) rescue ArgumentError redo end end 

He should keep calling new until all three random numbers are unique. But after I typed this code and ran it several times, I got this error: LocalJumpError: unexpected redo . I was looking for the redo keyword elsewhere, and it looks like it should only work for loops and iterators. So why did you try to use it in this example? How should this method be rewritten to work correctly?

+4
source share
2 answers

He should have used retry , not redo .

+9
source

redo restarts the block:

 l = lambda {puts "hi"; redo} l.call 
+3
source

All Articles