[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rescuing exceptions with "redo"

Damaris Fuentes

7/8/2006 7:37:00 PM

Hi aaall,

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

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

--
Posted via http://www.ruby-....

3 Answers

Ashley Moran

7/8/2006 7:50:00 PM

0


On Jul 08, 2006, at 8:37 pm, Damaris Fuentes wrote:

> 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

Damaris Fuentes

7/8/2006 8:56:00 PM

0

Yes! It works fine with retry! Thanks! :D

Ashley Moran wrote:
> On Jul 08, 2006, at 8:37 pm, Damaris Fuentes wrote:
>
>> 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


--
Posted via http://www.ruby-....

Julian 'Julik' Tarkhanov

7/12/2006 2:18:00 PM

0


On 8-jul-2006, at 21:37, Damaris Fuentes wrote:

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

Maybe he mistyped and meant "retry" instead?