From: Rodrigo Rosenfeld Rosas Date: 2011-01-20T23:28:04+09:00 Subject: [ruby-core:34663] Re: Returning from the callee Em 20-01-2011 08:21, Nikolai Weibull escreveu: > On Thu, Jan 20, 2011 at 02:00, Rodrigo Rosenfeld Rosas > wrote: >> Sometimes it is useful to be able to return from the callee method. For >> instance, consider a web application (how innovative, this is, right?): >> def update >> @m = Model.find(params[:id]) >> @m.attributes = params[:model] >> render text: 'invalid' unless @m.valid? >> render text: 'valid' >> end >> >> The experienced Rails user will know this will raise a DoubleRenderError >> exception (in Grails it would be worse, since no exception would be raised >> but the results would be concatenated). >> >> That is because, although we expect that "render" is the last thing to >> happen while dealing with web requests, render cannot return from the callee >> currently in Ruby. I guess Rails would implement it that way if this was >> allowed in Ruby. > Can you please provide a more elaborate example? I can���t deduce the > value of your proposal from it. Your example is just a convoluted > version of > > def update > ��� > render text: @m.valid? ? 'valid' : 'invalid' > end I was trying to be concise, but I guess it wasn't a good idea. Let me put this way: def update ... render text: 'invalid' unless @m.valid? # Several lines of code here if the request is valid render text: 'valid' end > (It���s not very localization-friendly either, but that���s another story ;-). > > (And, as already suggested, non-local exits are probably better > controlled with catch/throw.) I'll reply to that comment suggesting this approach later. (I'm a little bit in a hurry right now) Best regards, Rodrigo.