In the Poignant Guide this example of the redo
keyword is given:
class LotteryTicket
def self.new_random
new(rand(25) + 1, rand(25) + 1, rand(25) + 1)
rescue ArgumentError
redo
end
end
It's supposed to keep calling new
until all three random numbers are unique. But after I typed this code in and ran it a few times, I got this error: LocalJumpError: unexpected redo
. I looked up the redo
keyword elsewhere and it looks like it is only supposed to work for loops and iterators. So why did why try to use it like this in his example? How should this method be rewritten to work correctly?
He must have meant to use retry
, not redo
.
redo
restarts a block:
l = lambda {puts "hi"; redo}
l.call
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With