[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

return_on

Trans

1/10/2007 2:39:00 PM

Sometimes i do this:

return x if x

Anyway to code it as:

return_on x

t.


23 Answers

Pit Capitain

1/10/2007 3:59:00 PM

0

Trans schrieb:
> Sometimes i do this:
>
> return x if x
>
> Anyway to code it as:
>
> return_on x

It's not very nice, but you could wrap the whole method body in a block
and use throw/catch behind the scenes.

Regards,
Pit

Simon Strandgaard

1/10/2007 4:29:00 PM

0

On 1/10/07, Trans <transfire@gmail.com> wrote:
> Sometimes i do this:
>
> return x if x
>
> Anyway to code it as:
>
> return_on x
>

I cannot recall I ever have used return, in this way,
but my coding style may be 5% different than yours.
Maybe I can improve my minimal insight.

Any good (larger) examples of this thing?


--
Simon Strandgaard
http://opc...

Srinivas Jonnalagadda

1/11/2007 4:54:00 AM

0

On 10-Jan-2007, at 20:09, Trans wrote:
> Sometimes i do this:
>
> return x if x
>
> Anyway to code it as:
>
> return_on x
>
> t.

I do this quite often, as well! We need a macro system ;-)

JS

Devin Mullins

1/11/2007 5:36:00 AM

0

Trans wrote:
> Sometimes i do this:
> return x if x
> Anyway to code it as:
> return_on x

I'm wondering if continuations (or continuations + set_trace_func to
catch the method end) could be used to achieve that, but am not a whiz
at callcc, and am up past my bedtime (hence am not going to experiment
w/ it).

Is there a Weirich in the house?

Devin
(Even if it can be implemented using such hackery, I might consider that
a smell that it's so different, and doesn't map to Ruby's ways, and be
hesitant to use such a beast in GP code.)

Trans

1/13/2007 2:35:00 PM

0


Simon Strandgaard wrote:
> On 1/10/07, Trans <transfire@gmail.com> wrote:
> > Sometimes i do this:
> >
> > return x if x
> >
> > Anyway to code it as:
> >
> > return_on x
> >
>
> I cannot recall I ever have used return, in this way,
> but my coding style may be 5% different than yours.
> Maybe I can improve my minimal insight.
>
> Any good (larger) examples of this thing?

I don't really have any examples that are repleat with it, but as to
insight it's especially convenient when caching a return value:

def x
return_on @cache[:x]
# do stuff
@cache[:x] = result_of_stuff
end

this gets rid of an extraneous variable too b/c otherwise, the more
efficient impl. is:

def x
r = @cache[:x]
return r if r
# do stuff
@cache[:x] = result_of_stuff
end

funny thing i just came across a similar case for break. how would you
DRY this up and get rid of 'result'?

result = nil
files.each do
result = require file
break if result
end
if result
...

t.


Devin Mullins

1/13/2007 3:13:00 PM

0

Trans wrote:
> funny thing i just came across a similar case for break. how would you
> DRY this up and get rid of 'result'?
>
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...

Well, this is much easier:

if files.any? {|file| require file }
...

C'mon, Trans. :)

Devin
(Or am I missing something obvious?)

Pit Capitain

1/13/2007 4:49:00 PM

0

Trans schrieb:
> I don't really have any examples that are repleat with it, but as to
> insight it's especially convenient when caching a return value:
>
> def x
> return_on @cache[:x]
> # do stuff
> @cache[:x] = result_of_stuff
> end
>
> this gets rid of an extraneous variable too b/c otherwise, the more
> efficient impl. is:
>
> def x
> r = @cache[:x]
> return r if r
> # do stuff
> @cache[:x] = result_of_stuff
> end

You could also implement this as

def x
@cache[:x] ||= (
# do stuff
result_of_stuff
)
end

> funny thing i just came across a similar case for break. how would you
> DRY this up and get rid of 'result'?
>
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...

If "result" is only a flag whose value you don't need later, you could do

if files.find { |file| require file }
...
end

Regards,
Pit

Trans

1/13/2007 5:37:00 PM

0


Devin Mullins wrote:
> Trans wrote:
> > funny thing i just came across a similar case for break. how would you
> > DRY this up and get rid of 'result'?
> >
> > result = nil
> > files.each do
> > result = require file
> > break if result
> > end
> > if result
> > ...
>
> Well, this is much easier:
>
> if files.any? {|file| require file }
> ...

Cool, I forget that #any? will break after the first true encounter. I
used #find as pit suggested (which can return any value actually). But,
that wasn't really my point. Such a solution breaks the analogy to the
original return case --whihc can't be DRYed-up that way.

T.


Trans

1/13/2007 5:46:00 PM

0


Pit Capitain wrote:
> Trans schrieb:
> > I don't really have any examples that are repleat with it, but as to
> > insight it's especially convenient when caching a return value:
> >
> > def x
> > return_on @cache[:x]
> > # do stuff
> > @cache[:x] = result_of_stuff
> > end
> >
> > this gets rid of an extraneous variable too b/c otherwise, the more
> > efficient impl. is:
> >
> > def x
> > r = @cache[:x]
> > return r if r
> > # do stuff
> > @cache[:x] = result_of_stuff
> > end
>
> You could also implement this as
>
> def x
> @cache[:x] ||= (
> # do stuff
> result_of_stuff
> )
> end

That pretty good. I rarely use ( ) as local encapsulator and probably
should consider it more often. I'm generally partial to less indention
when I can get it though.

Thanks,
T.


Joel VanderWerf

1/13/2007 9:46:00 PM

0

Trans wrote:
...
> funny thing i just came across a similar case for break. how would you
> DRY this up and get rid of 'result'?
>
> result = nil
> files.each do
> result = require file
> break if result
> end
> if result
> ...

This only helps a little...

result = files.each do |file|
r = require file and break r
end

--
vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407