Rescuing exceptions with "redo"

Hi aaall,

I have a code like this (from Why’s (poignant) guide to Ruby, in
http://poignantguide.net/ruby/chapter-5.html#section5):


class LotteryTicket
NUMERIC_RANGE = 1…25

attr_reader :picks
def initialize( *picks )
if picks.length != 3
raise ArgumentError, “three numbers must be picked”
elsif picks.uniq.length != 3
raise ArgumentError, “the three picks must be different numbers”
elsif picks.detect { |p| not NUMERIC_RANGE === p }
raise ArgumentError, “the three picks must be numbers between 1
and 25.”
end
@picks = picks

end

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

end

ticket = LotteryTicket.new_random()


When the 3 random numbers created in this last line are not equal,
everything goes right. However, when two or three are identical, the
second argument error is raised and, theoretically, the method
“new_random” should be run automatically again. However, an exception
cames out:

in `new_random’: unexpected redo (LocalJumpError)

This exception should not be launched… (I thought). Suggestions?

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs