ruby-changes:48780
From: nobu <ko1@a...>
Date: Fri, 24 Nov 2017 14:01:00 +0900 (JST)
Subject: [ruby-changes:48780] nobu:r60896 (trunk): workspace.rb: fix SCRIPT_LINES__
nobu 2017-11-24 14:00:56 +0900 (Fri, 24 Nov 2017) New Revision: 60896 https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=60896 Log: 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 `$,`. Modified files: trunk/lib/irb/workspace.rb Index: lib/irb/workspace.rb =================================================================== --- lib/irb/workspace.rb (revision 60895) +++ lib/irb/workspace.rb (revision 60896) @@ -108,23 +108,21 @@ EOF https://github.com/ruby/ruby/blob/trunk/lib/irb/workspace.rb#L108 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 -- ML: ruby-changes@q... Info: http://www.atdot.net/~ko1/quickml/