From: Yusuke ENDOH Date: 2011-06-07T19:10:15+09:00 Subject: [ruby-core:36811] Re: [Ruby 1.9 - Feature #4840][Open] Allow returning from require Hello, 2011/6/6 Rodrigo Rosenfeld Rosas : > I have a situation where there is some code in Gitorious like: > > unless defined? GitoriousConfig > �# tons of lines here > end > > And I would it to look like > > return if defined? GitoriousConfig > > #tons of lines here > > It would be great to allow return from a required file or some other keyword (break, etc or a new one) Agreed. It would be also useful to write platform-specific code: require "test/unit" return unless /mswin|cygwin|mingw|bccwin/ =~ RUBY_PLATFORM class TestForWindowsEnv < Test::Unit::TestCase ... Here is an experimental patch: diff --git a/compile.c b/compile.c index 10d63bc..7b9c490 100644 --- a/compile.c +++ b/compile.c @@ -4291,10 +4291,6 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) rb_iseq_t *is = iseq; if (is) { - if (is->type == ISEQ_TYPE_TOP) { - COMPILE_ERROR((ERROR_ARGS "Invalid return")); - } - else { LABEL *splabel = 0; if (is->type == ISEQ_TYPE_METHOD) { @@ -4321,7 +4317,6 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped) ADD_INSN(ret, nd_line(node), pop); } } - } } break; } diff --git a/vm_insnhelper.c b/vm_insnhelper.c index f40dfdf..274f45d 100644 --- a/vm_insnhelper.c +++ b/vm_insnhelper.c @@ -1561,8 +1561,6 @@ vm_throw(rb_thread_t *th, rb_control_frame_t *reg_cfp, cfp = RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp); } - rb_vm_localjump_error("unexpected return", throwobj, TAG_RETURN); - valid_return: pt = dfp; } -- Yusuke Endoh