[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Rescuing blocks?

Daniel Nugent

2/12/2006 6:27:00 PM

Hey guys,

I was working on a DSL for some asynchronous programming stuff and I
realized it'd be really nice if a block could rescue an exception.

So I went into IRB to see if it works and got a parse error. I
suppose you need a begin... end block or a proper method to have a
rescue block right now.

I was wondering if there's any reason why this is so? It'd seem
pretty natural to me that a block could have rescue/else/ensure
conditions since a method body can have them.

Has this been discussed elsewhere?

Thanks,

--
-Dan Nugent


19 Answers

David Vallner

2/12/2006 6:45:00 PM

0

Dna Nedela 12 Február 2006 19:26 Daniel Nugent napísal:
> Hey guys,
>
> I was working on a DSL for some asynchronous programming stuff and I
> realized it'd be really nice if a block could rescue an exception.
>
> So I went into IRB to see if it works and got a parse error. I
> suppose you need a begin... end block or a proper method to have a
> rescue block right now.
>
> I was wondering if there's any reason why this is so? It'd seem
> pretty natural to me that a block could have rescue/else/ensure
> conditions since a method body can have them.
>
> Has this been discussed elsewhere?
>
> Thanks,
>
> --
> -Dan Nugent

I'm afraid I don't quite catch your drift. What do you mean by a block not
rescuing an exception? Some example code wouldn't hurt.

David Vallner


vanekl

2/12/2006 7:10:00 PM

0

this works,

foo = [1, 2, 3]

foo.each do |o|
begin
raise "Oh noes, it's number 2!" if o == 2
rescue Exception => e
puts e.to_s
end
end


Daniel Nugent wrote:

> Whoops, shoulda thought of that, a-doy.
>
> Okay, it's pretty simple, here's what I'd like to do (you can
> extrapolate the rest of the syntax from the simple example):
>
> foo = [1, 2, 3]
>
> foo.each do |o|
> raise "Oh noes, it's number 2!" if o == 2
> rescue Exception => e
> puts e.to_s
> end
>
> And so on and so forth. It seems pretty natural to me.... I don't
> think it breaks anything (least not off the top of my head)...
>
> On 2/12/06, David Vallner <david@vallner.net> wrote:
>
>>Dna Nedela 12 Február 2006 19:26 Daniel Nugent napísal:
>>
>>>Hey guys,
>>>
>>>I was working on a DSL for some asynchronous programming stuff and I
>>>realized it'd be really nice if a block could rescue an exception.
>>>
>>>So I went into IRB to see if it works and got a parse error. I
>>>suppose you need a begin... end block or a proper method to have a
>>>rescue block right now.
>>>
>>>I was wondering if there's any reason why this is so? It'd seem
>>>pretty natural to me that a block could have rescue/else/ensure
>>>conditions since a method body can have them.
>>>
>>>Has this been discussed elsewhere?
>>>
>>>Thanks,
>>>
>>>--
>>>-Dan Nugent
>>
>>I'm afraid I don't quite catch your drift. What do you mean by a block not
>>rescuing an exception? Some example code wouldn't hurt.
>>
>>David Vallner
>>
>>
>
>
>
> --
> -Dan Nugent



Mark Volkmann

2/12/2006 7:39:00 PM

0

On 2/12/06, Lou Vanek <vanek@acd.net> wrote:
> this works,
>
> foo = [1, 2, 3]
>
> foo.each do |o|
> begin
> raise "Oh noes, it's number 2!" if o == 2
> rescue Exception => e
> puts e.to_s
> end
> end

Sure it works, but Daniel wants to reduce the syntax a bit AND exit
the loop if an exception is raised without having to specify that. I
like his suggestion.

--
R. Mark Volkmann
Partner, Object Computing, Inc.


David Vallner

2/12/2006 8:24:00 PM

0

Dna Nedela 12 Február 2006 20:39 Mark Volkmann napísal:
> On 2/12/06, Lou Vanek <vanek@acd.net> wrote:
> > this works,
> >
> > foo = [1, 2, 3]
> >
> > foo.each do |o|
> > begin
> > raise "Oh noes, it's number 2!" if o == 2
> > rescue Exception => e
> > puts e.to_s
> > end
> > end
>
> Sure it works, but Daniel wants to reduce the syntax a bit AND exit
> the loop if an exception is raised without having to specify that. I
> like his suggestion.
>
> --
> R. Mark Volkmann
> Partner, Object Computing, Inc.

It would cause a little inconsistency with the curly brace form of blocks.
Either the rescue clause would have to work only for the do / end form, or
we'd be mixing braces and keywords in a single construct, which I couldn't
bear to look at.

David Vallner


Daniel Nugent

2/12/2006 8:44:00 PM

0

Isfoo.each {|o| raise "Oh noes, it's number 2!" if o == 2rescue Exception => e puts e.to_s}really that hideous?On 2/12/06, David Vallner <david@vallner.net> wrote:> Dna Nedela 12 Február 2006 20:39 Mark Volkmann napísal:> > On 2/12/06, Lou Vanek <vanek@acd.net> wrote:> > > this works,> > >> > > foo = [1, 2, 3]> > >> > > foo.each do |o|> > > begin> > > raise "Oh noes, it's number 2!" if o == 2> > > rescue Exception => e> > > puts e.to_s> > > end> > > end> >> > Sure it works, but Daniel wants to reduce the syntax a bit AND exit> > the loop if an exception is raised without having to specify that. I> > like his suggestion.> >> > --> > R. Mark Volkmann> > Partner, Object Computing, Inc.>> It would cause a little inconsistency with the curly brace form of blocks.> Either the rescue clause would have to work only for the do / end form, or> we'd be mixing braces and keywords in a single construct, which I couldn't> bear to look at.>> David Vallner>>---Dan Nugent

David Vallner

2/12/2006 9:27:00 PM

0

How would you parse the difference whether the raise applies to the whole
block, or just the previous statement?

David Vallner

Dna Nedela 12 Február 2006 21:43 Daniel Nugent napísal:
> Is
>
> foo.each {|o|
> raise "Oh noes, it's number 2!" if o == 2
> rescue Exception => e
> puts e.to_s
> }
>
> really that hideous?
>
> On 2/12/06, David Vallner <david@vallner.net> wrote:
> > Dna Nedela 12 Február 2006 20:39 Mark Volkmann napísal:
> > > On 2/12/06, Lou Vanek <vanek@acd.net> wrote:
> > > > this works,
> > > >
> > > > foo = [1, 2, 3]
> > > >
> > > > foo.each do |o|
> > > > begin
> > > > raise "Oh noes, it's number 2!" if o == 2
> > > > rescue Exception => e
> > > > puts e.to_s
> > > > end
> > > > end
> > >
> > > Sure it works, but Daniel wants to reduce the syntax a bit AND exit
> > > the loop if an exception is raised without having to specify that. I
> > > like his suggestion.
> > >
> > > --
> > > R. Mark Volkmann
> > > Partner, Object Computing, Inc.
> >
> > It would cause a little inconsistency with the curly brace form of
> > blocks. Either the rescue clause would have to work only for the do / end
> > form, or we'd be mixing braces and keywords in a single construct, which
> > I couldn't bear to look at.
> >
> > David Vallner
>
> --
> -Dan Nugent


David Vallner

2/12/2006 10:11:00 PM

0



Dna Nedela 12 Február 2006 22:46 Daniel Nugent napísal:
> Sorry, not sure what the issue is (I assume you mean rescue, not raise
> there).
>
> If a rescue appears after a statement, it only applies to that
> statement. If a rescue appears as the first token on a line, it
> applies to the enclosing block. I'm pretty sure that's how it works
> now.

<dense>D'oh. Didn't know that one... </dense>


David Vallner


Patrick Hurley

2/13/2006 4:21:00 PM

0

On 2/12/06, David Vallner <david@vallner.net> wrote:>>> Dna Nedela 12 Február 2006 22:46 Daniel Nugent napísal:> > Sorry, not sure what the issue is (I assume you mean rescue, not raise> > there).> >> > If a rescue appears after a statement, it only applies to that> > statement. If a rescue appears as the first token on a line, it> > applies to the enclosing block. I'm pretty sure that's how it works> > now.>> <dense>D'oh. Didn't know that one... </dense>>>> David Vallner>>+1

Daniel Nugent

2/13/2006 7:30:00 PM

0

So, maybe this calls for a RCR?I know for sure that I'd like blocks to be rescuable.On 2/13/06, Patrick Hurley <phurley@gmail.com> wrote:> On 2/12/06, David Vallner <david@vallner.net> wrote:> >> >> > Dna Nedela 12 Február 2006 22:46 Daniel Nugent napísal:> > > Sorry, not sure what the issue is (I assume you mean rescue, not raise> > > there).> > >> > > If a rescue appears after a statement, it only applies to that> > > statement. If a rescue appears as the first token on a line, it> > > applies to the enclosing block. I'm pretty sure that's how it works> > > now.> >> > <dense>D'oh. Didn't know that one... </dense>> >> >> > David Vallner> >> >>> +1>---Dan Nugent

David Vallner

2/14/2006 9:28:00 PM

0

Dna Pondelok 13 Február 2006 17:20 Patrick Hurley napísal:
> +1
>

Die.

David Vallner
Allergic to anything remotely resembling slashdot