From: Rob Sanheim Date: 2007-01-15T08:26:47+09:00 Subject: Re: return_on On 1/13/07, Trans wrote: > > Simon Strandgaard wrote: > > On 1/10/07, Trans 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. Much like returning in ActiveSupport: http://weblog.jamisbuck.org/2006/10/27/mining-activesupport-object-returning - rob