Skip to content

Commit

Permalink
Move main object's safe call logic to Context (#1034)
Browse files Browse the repository at this point in the history
  • Loading branch information
st0012 authored Nov 20, 2024
1 parent 2f1c593 commit 9750fa2
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
9 changes: 2 additions & 7 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1463,21 +1463,16 @@ def truncate_prompt_main(str) # :nodoc:
end
end

def basic_object_safe_main_call(method)
main = @context.main
Object === main ? main.__send__(method) : Object.instance_method(method).bind_call(main)
end

def format_prompt(format, ltype, indent, line_no) # :nodoc:
format.gsub(/%([0-9]+)?([a-zA-Z%])/) do
case $2
when "N"
@context.irb_name
when "m"
main_str = basic_object_safe_main_call(:to_s) rescue "!#{$!.class}"
main_str = @context.safe_method_call_on_main(:to_s) rescue "!#{$!.class}"
truncate_prompt_main(main_str)
when "M"
main_str = basic_object_safe_main_call(:inspect) rescue "!#{$!.class}"
main_str = @context.safe_method_call_on_main(:inspect) rescue "!#{$!.class}"
truncate_prompt_main(main_str)
when "l"
ltype
Expand Down
5 changes: 5 additions & 0 deletions lib/irb/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -704,5 +704,10 @@ def inspect # :nodoc:
def local_variables # :nodoc:
workspace.binding.local_variables
end

def safe_method_call_on_main(method_name)
main_object = main
Object === main_object ? main_object.__send__(method_name) : Object.instance_method(method_name).bind_call(main_object)
end
end
end

0 comments on commit 9750fa2

Please sign in to comment.