root/lang/ruby/misc/shystd/lib/binding.rb @ 30949

Revision 30949, 2.1 kB (checked in by shyouhei, 6 years ago)

Binding#eval backported to 1.8.7

Line 
1#!/bin/ruby -Ku
2
3class Binding
4        # First appear: RCR251
5        def eval str, *remain
6                Kernel.eval str, self, *remain
7        end
8end if RUBY_VERSION < '1.8.7'
9
10# code below belongs to me:
11# Copyright(c) 2006 URABE, Shyouhei.
12#
13# Permission is hereby granted, free of  charge, to any person obtaining a copy
14# of  this code, to  deal in  the code  without restriction,  including without
15# limitation  the rights  to  use, copy,  modify,  merge, publish,  distribute,
16# sublicense, and/or sell copies of the code, and to permit persons to whom the
17# code is furnished to do so, subject to the following conditions:
18#
19#     The above copyright notice and this permission notice shall be
20#     included in all copies or substantial portions of the code.
21#
22# THE  CODE IS  PROVIDED "AS  IS",  WITHOUT WARRANTY  OF ANY  KIND, EXPRESS  OR
23# IMPLIED,  INCLUDING BUT  NOT LIMITED  TO THE  WARRANTIES  OF MERCHANTABILITY,
24# FITNESS FOR A PARTICULAR PURPOSE  AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25# AUTHOR  OR  COPYRIGHT  HOLDER BE  LIABLE  FOR  ANY  CLAIM, DAMAGES  OR  OTHER
26# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27# OUT OF  OR IN CONNECTION WITH  THE CODE OR THE  USE OR OTHER  DEALINGS IN THE
28# CODE.
29
30class Binding
31        # Binding-of-caller.     Note this      method erases the tracer func already being
32        # set.  Thus, you cannot nest Binding.of_caller.
33        def self.of_caller # :yields: bnd
34                bnd = callcc do |cc|
35                        lv3 = lambda do |e, f, l, m, b, c|
36                                set_trace_func nil
37                                cc.call b
38                        end
39                        lv2 = lambda do |e, f, l, m, b, c|
40                                set_trace_func lv3 if e == "return"
41                        end
42                        lv1 = lambda do |e, f, l, m, b, c|
43                                set_trace_func lv2 if e == "return" && m == :of_caller
44                        end
45                        set_trace_func lv1
46                        return
47                end
48                yield bnd
49        end
50
51        # get a value bound to variable _k_ in this binding.
52        # note you cannot set a new value to this variable
53        def [] k
54                self.eval k.to_s
55        end
56
57        # variables in this binding
58        def local_variables
59                self.eval "local_variables"
60        end
61end
62
63
64# Local Variables:
65# mode: ruby
66# coding: utf-8
67# indent-tabs-mode: t
68# tab-width: 3
69# ruby-indent-level: 3
70# fill-column: 79
71# default-justification: full
72# End:
73# vi: ts=3 sw=3
Note: See TracBrowser for help on using the browser.