Skip to content

Commit 2df2e86

Browse files
st0012matzbot
authored andcommitted
[ruby/irb] Move main object's safe call logic to Context
(ruby/irb#1034) ruby/irb@9750fa23cc
1 parent 743a31d commit 2df2e86

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

lib/irb.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1463,21 +1463,16 @@ def truncate_prompt_main(str) # :nodoc:
14631463
end
14641464
end
14651465

1466-
def basic_object_safe_main_call(method)
1467-
main = @context.main
1468-
Object === main ? main.__send__(method) : Object.instance_method(method).bind_call(main)
1469-
end
1470-
14711466
def format_prompt(format, ltype, indent, line_no) # :nodoc:
14721467
format.gsub(/%([0-9]+)?([a-zA-Z%])/) do
14731468
case $2
14741469
when "N"
14751470
@context.irb_name
14761471
when "m"
1477-
main_str = basic_object_safe_main_call(:to_s) rescue "!#{$!.class}"
1472+
main_str = @context.safe_method_call_on_main(:to_s) rescue "!#{$!.class}"
14781473
truncate_prompt_main(main_str)
14791474
when "M"
1480-
main_str = basic_object_safe_main_call(:inspect) rescue "!#{$!.class}"
1475+
main_str = @context.safe_method_call_on_main(:inspect) rescue "!#{$!.class}"
14811476
truncate_prompt_main(main_str)
14821477
when "l"
14831478
ltype

lib/irb/context.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,5 +704,10 @@ def inspect # :nodoc:
704704
def local_variables # :nodoc:
705705
workspace.binding.local_variables
706706
end
707+
708+
def safe_method_call_on_main(method_name)
709+
main_object = main
710+
Object === main_object ? main_object.__send__(method_name) : Object.instance_method(method_name).bind_call(main_object)
711+
end
707712
end
708713
end

0 commit comments

Comments
 (0)