summaryrefslogtreecommitdiff
path: root/vm_eval.c
diff options
context:
space:
mode:
Diffstat (limited to 'vm_eval.c')
-rw-r--r--vm_eval.c41
1 files changed, 41 insertions, 0 deletions
diff --git a/vm_eval.c b/vm_eval.c
index 8e6bd76e9e..e9774d2785 100644
--- a/vm_eval.c
+++ b/vm_eval.c
@@ -1377,11 +1377,52 @@ rb_f_local_variables(void)
return ary;
}
+/*
+ * call-seq:
+ * block_given? => true or false
+ * iterator? => true or false
+ *
+ * Returns <code>true</code> if <code>yield</code> would execute a
+ * block in the current context. The <code>iterator?</code> form
+ * is mildly deprecated.
+ *
+ * def try
+ * if block_given?
+ * yield
+ * else
+ * "no block"
+ * end
+ * end
+ * try #=> "no block"
+ * try { "hello" } #=> "hello"
+ * try do "hello" end #=> "hello"
+ */
+
+
+VALUE
+rb_f_block_given_p(void)
+{
+ rb_thread_t *th = GET_THREAD();
+ rb_control_frame_t *cfp = th->cfp;
+ cfp = vm_get_ruby_level_caller_cfp(th, RUBY_VM_PREVIOUS_CONTROL_FRAME(cfp));
+
+ if (cfp != 0 &&
+ (cfp->lfp[0] & 0x02) == 0 &&
+ GC_GUARDED_PTR_REF(cfp->lfp[0])) {
+ return Qtrue;
+ }
+ else {
+ return Qfalse;
+ }
+}
+
void
Init_vm_eval(void)
{
rb_define_global_function("eval", rb_f_eval, -1);
rb_define_global_function("local_variables", rb_f_local_variables, 0);
+ rb_define_global_function("iterator?", rb_f_block_given_p, 0);
+ rb_define_global_function("block_given?", rb_f_block_given_p, 0);
rb_define_global_function("catch", rb_f_catch, -1);
rb_define_global_function("throw", rb_f_throw, -1);