File tree Expand file tree Collapse file tree 2 files changed +40
-20
lines changed Expand file tree Collapse file tree 2 files changed +40
-20
lines changed Original file line number Diff line number Diff line change @@ -50,16 +50,18 @@ def search_file_from_ruby_path(fn) # :nodoc:
50
50
# See Irb#suspend_input_method for more information.
51
51
def source_file ( path )
52
52
irb . suspend_name ( path , File . basename ( path ) ) do
53
- irb . suspend_input_method ( FileInputMethod . new ( path ) ) do
54
- |back_io |
55
- irb . signal_status ( :IN_LOAD ) do
56
- if back_io . kind_of? ( FileInputMethod )
57
- irb . eval_input
58
- else
59
- begin
53
+ FileInputMethod . open ( path ) do |io |
54
+ irb . suspend_input_method ( io ) do
55
+ |back_io |
56
+ irb . signal_status ( :IN_LOAD ) do
57
+ if back_io . kind_of? ( FileInputMethod )
60
58
irb . eval_input
61
- rescue LoadAbort
62
- print "load abort!!\n "
59
+ else
60
+ begin
61
+ irb . eval_input
62
+ rescue LoadAbort
63
+ print "load abort!!\n "
64
+ end
63
65
end
64
66
end
65
67
end
@@ -79,16 +81,18 @@ def load_file(path, priv = nil)
79
81
ws = WorkSpace . new
80
82
end
81
83
irb . suspend_workspace ( ws ) do
82
- irb . suspend_input_method ( FileInputMethod . new ( path ) ) do
83
- |back_io |
84
- irb . signal_status ( :IN_LOAD ) do
85
- if back_io . kind_of? ( FileInputMethod )
86
- irb . eval_input
87
- else
88
- begin
84
+ FileInputMethod . open ( path ) do |io |
85
+ irb . suspend_input_method ( io ) do
86
+ |back_io |
87
+ irb . signal_status ( :IN_LOAD ) do
88
+ if back_io . kind_of? ( FileInputMethod )
89
89
irb . eval_input
90
- rescue LoadAbort
91
- print "load abort!!\n "
90
+ else
91
+ begin
92
+ irb . eval_input
93
+ rescue LoadAbort
94
+ print "load abort!!\n "
95
+ end
92
96
end
93
97
end
94
98
end
Original file line number Diff line number Diff line change @@ -124,10 +124,22 @@ def inspect
124
124
125
125
# Use a File for IO with irb, see InputMethod
126
126
class FileInputMethod < InputMethod
127
+ class << self
128
+ def open ( file , &block )
129
+ begin
130
+ io = new ( file )
131
+ block . call ( io )
132
+ ensure
133
+ io &.close
134
+ end
135
+ end
136
+ end
137
+
127
138
# Creates a new input method object
128
139
def initialize ( file )
129
140
super
130
141
@io = IRB ::MagicFile . open ( file )
142
+ @external_encoding = @io . external_encoding
131
143
end
132
144
# The file name of this input method, usually given during initialization.
133
145
attr_reader :file_name
@@ -137,7 +149,7 @@ def initialize(file)
137
149
#
138
150
# See IO#eof? for more information.
139
151
def eof?
140
- @io . eof?
152
+ @io . closed? || @io . eof?
141
153
end
142
154
143
155
# Reads the next line from this input method.
@@ -150,13 +162,17 @@ def gets
150
162
151
163
# The external encoding for standard input.
152
164
def encoding
153
- @io . external_encoding
165
+ @external_encoding
154
166
end
155
167
156
168
# For debug message
157
169
def inspect
158
170
'FileInputMethod'
159
171
end
172
+
173
+ def close
174
+ @io . close
175
+ end
160
176
end
161
177
162
178
begin
You can’t perform that action at this time.
0 commit comments