OSDN Git Service

FIRST REPOSITORY
[eos/hostdependOTHERS.git] / I386LINUX / util / I386LINUX / lib / ruby / 1.6 / irb / extend-command.rb
1 #
2 #   irb/extend-command.rb - irb command extend
3 #       $Release Version: 0.7.3$
4 #       $Revision: 1.1.1.1 $
5 #       $Date: 2002/02/01 06:36:22 $
6 #       by Keiju ISHITSUKA(keiju@ishitsuka.com)
7 #
8 # --
9 #
10 #   
11 #
12 module IRB
13   #
14   # IRB extended command
15   #
16   module ExtendCommand
17 #    include Loader
18     
19     def irb_exit(ret = 0)
20       irb_context.exit(ret)
21     end
22     alias irb_quit irb_exit
23
24     def irb_fork(&block)
25       pid = send ExtendCommand.irb_original_method_name("fork")
26       unless pid 
27         class<<self
28           alias_method :exit, ExtendCommand.irb_original_method_name('exit')
29         end
30         if iterator?
31           begin
32             yield
33           ensure
34             exit
35           end
36         end
37       end
38       pid
39     end
40
41     def irb_change_binding(*main)
42       irb_context.change_binding(*main)
43     end
44     alias irb_change_workspace irb_change_binding
45
46     def irb_source(file)
47       irb_context.source(file)
48     end
49
50     def irb(*obj)
51       require "irb/multi-irb"
52       IRB.irb(nil, *obj)
53     end
54
55     def irb_context
56       IRB.conf[:MAIN_CONTEXT]
57     end
58
59     def irb_jobs
60       require "irb/multi-irb"
61       IRB.JobManager
62     end
63
64     def irb_fg(key)
65       require "irb/multi-irb"
66       IRB.JobManager.switch(key)
67     end
68
69     def irb_kill(*keys)
70       require "irb/multi-irb"
71       IRB.JobManager.kill(*keys)
72     end
73
74     # extend command functions
75     def ExtendCommand.extend_object(obj)
76       super
77       unless (class<<obj;ancestors;end).include?(ExtendCommand)
78         obj.install_aliases
79       end
80     end
81
82     OVERRIDE_NOTHING = 0
83     OVERRIDE_PRIVATE_ONLY = 0x01
84     OVERRIDE_ALL = 0x02
85
86     def install_aliases(override = OVERRIDE_NOTHING)
87
88       install_alias_method(:exit, :irb_exit, override | OVERRIDE_PRIVATE_ONLY)
89       install_alias_method(:quit, :irb_quit, override | OVERRIDE_PRIVATE_ONLY)
90       install_alias_method(:fork, :irb_fork, override | OVERRIDE_PRIVATE_ONLY)
91       install_alias_method(:kill, :irb_kill, override | OVERRIDE_PRIVATE_ONLY)
92
93       install_alias_method(:irb_cb, :irb_change_binding, override)
94       install_alias_method(:irb_ws, :irb_change_workspace, override)
95       install_alias_method(:source, :irb_source, override)
96       install_alias_method(:conf, :irb_context, override)
97       install_alias_method(:jobs, :irb_jobs, override)
98       install_alias_method(:fg, :irb_fg, override)
99     end
100
101     # override = {OVERRIDE_NOTHING, OVERRIDE_PRIVATE_ONLY, OVERRIDE_ALL}
102     def install_alias_method(to, from, override = OVERRIDE_NOTHING)
103       to = to.id2name unless to.kind_of?(String)
104       from = from.id2name unless from.kind_of?(String)
105
106       if override == OVERRIDE_ALL or
107           (override == OVERRIDE_PRIVATE_ONLY) && !respond_to?(to) or
108           (override == OVERRIDE_NOTHING) &&  !respond_to?(to, true)
109         target = self
110         (class<<self;self;end).instance_eval{
111           if target.respond_to?(to, true) && 
112               !target.respond_to?(ExtendCommand.irb_original_method_name(to), true)
113             alias_method(ExtendCommand.irb_original_method_name(to), to) 
114           end
115           alias_method to, from
116         }
117       else
118         print "irb: warn: can't alias #{to} from #{from}.\n"
119       end
120     end
121
122     def self.irb_original_method_name(method_name)
123       "irb_" + method_name + "_org"
124     end
125   end
126 end