aboutsummaryrefslogtreecommitdiffstats
path: root/lib/irb
diff options
context:
space:
mode:
authork0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
committerk0kubun <k0kubun@b2dd03c8-39d4-4d8f-98ff-823fe69b080e>2017-11-24 03:53:27 +0000
commit6f2e59d64e4ce2b1fdd2ca05c202d3e5ecb70450 (patch)
treeec8528ed8bf060ad891770220295422bbd8a62da /lib/irb
parentea03268471d9b234f379ab9abdec9341e2bcb976 (diff)
downloadruby-6f2e59d64e4ce2b1fdd2ca05c202d3e5ecb70450.tar.gz
irb.rb: show source around binding.irb on start
[Feature #14124] [ruby-dev:50319] [close GH-1764] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@60888 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/irb')
-rw-r--r--lib/irb/workspace.rb21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/irb/workspace.rb b/lib/irb/workspace.rb
index 9051f96df0..5c1e97a981 100644
--- a/lib/irb/workspace.rb
+++ b/lib/irb/workspace.rb
@@ -107,6 +107,27 @@ EOF
bt
end
+ def code_around_binding
+ file = @binding.eval('__FILE__')
+ pos = @binding.eval('__LINE__') - 1
+ return nil unless File.exist?(file)
+
+ if defined?(SCRIPT_LINES__) && SCRIPT_LINES__.key?(file)
+ lines = SCRIPT_LINES__[file].split(/^/)
+ else
+ lines = File.readlines(file)
+ end
+
+ start_pos = [pos - 5, 0].max
+ end_pos = [pos + 5, lines.size - 1].min
+
+ 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
+ "\nFrom: #{file} @ line #{pos + 1} :\n\n#{body}\n"
+ end
+
def IRB.delete_caller
end
end