diff options
author | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-24 05:00:56 +0000 |
---|---|---|
committer | nobu <nobu@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2017-11-24 05:00:56 +0000 |
commit | 8b6e9aa792a7a5b2c7f56ba8590d86473f406fad (patch) | |
tree | 9f162b9c0598d5c6c2d2d94fbc872618686aa9b3 /lib/irb | |
parent | 8fb87e43c6f5f176fadc74fe51b98d0ddc0f6d61 (diff) | |
download | ruby-8b6e9aa792a7a5b2c7f56ba8590d86473f406fad.tar.gz |
workspace.rb: fix SCRIPT_LINES__
* lib/irb/workspace.rb (code_around_binding): `SCRIPT_LINES__`
values are arrays of lines. get file and line at once. moved
loop-invariant format string. join without extra strings by
`$,`.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60896 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/irb')
-rw-r--r-- | lib/irb/workspace.rb | 16 |
1 files changed, 7 insertions, 9 deletions
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb index 5c1e97a981..63f0de6353 100644 --- a/lib/irb/workspace.rb +++ b/lib/irb/workspace.rb @@ -108,23 +108,21 @@ EOF end def code_around_binding - file = @binding.eval('__FILE__') - pos = @binding.eval('__LINE__') - 1 - return nil unless File.exist?(file) + file, pos = @binding.eval('[__FILE__, __LINE__]') - if defined?(SCRIPT_LINES__) && SCRIPT_LINES__.key?(file) - lines = SCRIPT_LINES__[file].split(/^/) - else + unless defined?(::SCRIPT_LINES__[file]) && lines = ::SCRIPT_LINES__[file] + return unless File.exist?(file) lines = File.readlines(file) end + pos -= 1 start_pos = [pos - 5, 0].max end_pos = [pos + 5, lines.size - 1].min + fmt = "%2s %#{end_pos.to_s.length}d: %s" body = (start_pos..end_pos).map do |current_pos| - lineno = "%#{end_pos.to_s.length}d" % (current_pos + 1) - " #{pos == current_pos ? '=>' : ' '} #{lineno}: #{lines[current_pos]}" - end.join + sprintf(fmt, pos == current_pos ? '=>' : '', current_pos + 1, lines[current_pos]) + end.join("") "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}\n" end |