Rescuing exceptions with "redo"

  • Thread starter Damaris Fuentes
  • Start date
D

Damaris Fuentes

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?
 
A

Ashley Moran

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?


He must have meant to type "retry" instead of "redo". redo is for
loops only AFAIK

Ashley
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,001
Messages
2,570,254
Members
46,850
Latest member
VMRKlaus8

Latest Threads

Top