[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Using "rescue" inline

Iñaki Baz Castillo

2/25/2009 5:12:00 PM

Hi, is it correct the following "rescue" usage?

myvar =3D 1 # Fixnum
othervar =3D myvar.downcase rescue myvar
=3D> 1

It seems to work, but is it really correct? Thanks.

--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>

8 Answers

Brian Adkins

2/25/2009 5:59:00 PM

0

Iñaki Baz Castillo <ibc@aliax.net> writes:

> Hi, is it correct the following "rescue" usage?
>
> myvar = 1 # Fixnum
> othervar = myvar.downcase rescue myvar
> => 1
>
> It seems to work, but is it really correct? Thanks.

Naturally, "correctness" depends on what you're trying to
accomplish. That is one valid use of rescue as a statement modifier
which will assign the value of myvar.downcase to othervar unless the
downcase method raises an exception, in which case it will assign
myvar instead. You can also use more than one rescue as in:

x = foo(y) rescue bar(y) rescue baz(y) rescue nil

--
Brian Adkins
http://...

Brian Candler

2/25/2009 6:06:00 PM

0

Iñaki Baz Castillo wrote:
> Hi, is it correct the following "rescue" usage?
>
> myvar = 1 # Fixnum
> othervar = myvar.downcase rescue myvar
> => 1
>
> It seems to work, but is it really correct? Thanks.

Yes. What you have written is shorthand form of rescue, equivalent to:

myvar = 1
othervar = begin
myvar.downcase
rescue StandardError
myvar
end
--
Posted via http://www.ruby-....

Rob Biedenharn

2/25/2009 6:07:00 PM

0

On Feb 25, 2009, at 12:11 PM, I=F1aki Baz Castillo wrote:

> Hi, is it correct the following "rescue" usage?
>
> myvar =3D 1 # Fixnum
> othervar =3D myvar.downcase rescue myvar
> =3D> 1
>
> It seems to work, but is it really correct? Thanks.
>
> -- =20
> I=F1aki Baz Castillo
> <ibc@aliax.net>


Hmm, "correct"? Well, it is certainly legal syntax and is equivalent to:

othervar =3D begin
myvar.downcase
rescue
myvar
end

But, it might not be the best way to shave the yak.

othervar =3D myvar.respond_to?(:downcase) ? myvar.downcase : myvar

might perform better if the exception to be rescued is expensive to =20
construct only to then be thrown away. (I don't know if there's any =20
special optimization of the expression form of rescue compared to the =20=

block form.)

It also can hide problems that you won't know that you have. For =20
example,
myvar =3D nil
othervar =3D myvar.downcase rescue myvar
Did you want to set othervar to nil also?
Perhaps you need othervar as a downcased String? Maybe it would be =20
better as:
othervar =3D myvar.to_s.downcase

In any case, you'd have to decide.

-Rob


Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com=

Herman Martinelli

2/25/2009 6:47:00 PM

0

Brian Adkins wrote:
> Naturally, "correctness" depends on what you're trying to
> accomplish.

LOL
That's really a good one (in addition to the valuable Ruby answer).

H.

Iñaki Baz Castillo

2/25/2009 7:54:00 PM

0

El Mi=E9rcoles, 25 de Febrero de 2009, Rob Biedenharn escribi=F3:

> othervar =3D myvar.respond_to?(:downcase) ? myvar.downcase : myvar
>
> might perform better if the exception to be rescued is expensive to
> construct only to then be thrown away. (I don't know if there's any
> special optimization of the expression form of rescue compared to the
> block form.)

Really good point. But I've done some benchmarks comparing both approache a=
nd=20
using "rescue" is ~ 0.30e-5 faster than your approach (even if yours seems=
=20
more ellegant to me).


> It also can hide problems that you won't know that you have. For
> example,
> myvar =3D nil
> othervar =3D myvar.downcase rescue myvar
> Did you want to set othervar to nil also?

Not exactly, but myvar could be a Fixnum so othervar would also be a Fixnum=
=2E=20
But if myvar is a String then I want othervar to be the same String but=20
downcase.


Thanks a lot.


=2D-=20
I=F1aki Baz Castillo

Rob Biedenharn

2/25/2009 8:40:00 PM

0

On Feb 25, 2009, at 2:54 PM, I=F1aki Baz Castillo wrote:

> El Mi=E9rcoles, 25 de Febrero de 2009, Rob Biedenharn escribi=F3:
>
>> othervar =3D myvar.respond_to?(:downcase) ? myvar.downcase : myvar
>>
>> might perform better if the exception to be rescued is expensive to
>> construct only to then be thrown away. (I don't know if there's any
>> special optimization of the expression form of rescue compared to the
>> block form.)
>
> Really good point. But I've done some benchmarks comparing both =20
> approache and
> using "rescue" is ~ 0.30e-5 faster than your approach (even if yours =20=

> seems
> more ellegant to me).

It depends on how often myvar.downcase raises an exception. If that's =20=

really an exceptional occurrence, then just rescuing the occasional =20
failure is the Ruby way.

>> It also can hide problems that you won't know that you have. For
>> example,
>> myvar =3D nil
>> othervar =3D myvar.downcase rescue myvar
>> Did you want to set othervar to nil also?
>
> Not exactly, but myvar could be a Fixnum so othervar would also be a =20=

> Fixnum.
> But if myvar is a String then I want othervar to be the same String =20=

> but
> downcase.
>
>
> Thanks a lot.
> --=20
> I=F1aki Baz Castillo


de nada

-Rob

Rob Biedenharn http://agileconsult...
Rob@AgileConsultingLLC.com



lasitha

2/26/2009 11:26:00 AM

0

On Thu, Feb 26, 2009 at 1:24 AM, I=F1aki Baz Castillo <ibc@aliax.net> wrote=
:
>> It also can hide problems that you won't know that you have. For
>> example,
>> =A0 =A0myvar =3D nil
>> =A0 =A0othervar =3D myvar.downcase rescue myvar
>> Did you want to set othervar to nil also?
>
> Not exactly, but myvar could be a Fixnum so othervar would also be a Fixn=
um.
> But if myvar is a String then I want othervar to be the same String but
> downcase.

This may be stating the obvious but code that branches based on type
is smelly. That is not to say it's always wrong, just always at least
a little smelly :). Particularly if you find multiple places in the
code having to deal with Fixnum and String conditionally.

The inline rescue slightly obscures the conditional but the smell
remains. It may be worthwhile reconsidering whether you really want
myvar to hold disparate types.

I would also reiterate Rob's concern that this rescue could easily
hide some unrelated bug (mvay being nil being the most obvious).

These two concerns would be enough for me to reconsider, but as usual,
it all depends on context.

Cheers,
lasitha

Iñaki Baz Castillo

2/26/2009 2:30:00 PM

0

2009/2/26 lasitha <lasitha.ranatunga@gmail.com>:
> On Thu, Feb 26, 2009 at 1:24 AM, I=C3=B1aki Baz Castillo <ibc@aliax.net> =
wrote:
>>> It also can hide problems that you won't know that you have. For
>>> example,
>>> =C2=A0 =C2=A0myvar =3D nil
>>> =C2=A0 =C2=A0othervar =3D myvar.downcase rescue myvar
>>> Did you want to set othervar to nil also?
>>
>> Not exactly, but myvar could be a Fixnum so othervar would also be a Fix=
num.
>> But if myvar is a String then I want othervar to be the same String but
>> downcase.
>
> This may be stating the obvious but code that branches based on type
> is smelly. =C2=A0That is not to say it's always wrong, just always at lea=
st
> a little smelly :). =C2=A0Particularly if you find multiple places in the
> code having to deal with Fixnum and String conditionally.
>
> The inline rescue slightly obscures the conditional but the smell
> remains. =C2=A0It may be worthwhile reconsidering whether you really want
> myvar to hold disparate types.
>
> I would also reiterate Rob's concern that this rescue could easily
> hide some unrelated bug (mvay being nil being the most obvious).
>
> These two concerns would be enough for me to reconsider, but as usual,
> it all depends on context.

Yes, you are right, I'll consider it. Thanks a lot.


--=20
I=C3=B1aki Baz Castillo
<ibc@aliax.net>