Since Ruby is built on top of simple concepts, most of the documentation
can be read in the Core API and Standard Library API pages.
Unfortunately, I don't see to find any official reference (besides
mentions to books) about the language syntax itself.
For instance, I wanted to know if this is possible:
class MyViewRenderer < DefaultRender # DefaultRender defines the
'render' method
def list
render_and_return[view: 'not_allowed'] unless allowed?
render view: 'list' # shouldn't get called
end
protected
def render_and_return
proc {|*args| render *args; return }
end
def allowed?
false
end
end
Yeah, I know several folks will point me out that I should be using
catch-throw for achieving something like this, but that is not the point.
What I'm saying is that I can't find any official reference about the
meanings of "return", "next" or "break", for instance. Nor can I find
the reason why such construction is not allowed.
So, what is the reason why the proc with a return can only be called
inside the method that created it?
Also, is there any documentation source that I'm missing?
Cheers,
Rodrigo.
P.S:
With regards to the catch-throw alternative, it means that I would need
to have control over the class calling the method from MyViewRenderer,
which may not be the case... What to do then?
I'm trying to avoid writing ugly code like this:
(render(...); return) if condition?
Or
if condition?
render(...)
return
end
Also, unless the API of DefaultRender explicitly says that render will
always return a truthy value, I couldn't write something like
render(...) and return if condition?