summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomoya ishida <[email protected]>2024-11-19 22:17:07 +0900
committergit <[email protected]>2024-11-19 13:17:11 +0000
commitbc9237966412c87d0a2d64b2787d2a39a04eca65 ()
treeba8fa77916ffcdf4d543fc38aaa742f2ba109a01
parentedf5a738a36fccd07ea79d825bda0f8bd56224a2 (diff)
[ruby/irb] Don't use delegator to install helper methods to main
object (https://.com/ruby/irb/pull/1031) IRB used delegator to install command as a method of frozen main object. Command is not a method now. We can drop it. https://.com/ruby/irb/commit/2f1c593801
-rw-r--r--lib/irb.rb9
-rw-r--r--lib/irb/command/internal_helpers.rb2
-rw-r--r--lib/irb/completion.rb3
-rw-r--r--lib/irb/workspace.rb32
-rw-r--r--test/irb/command/test_cd.rb19
-rw-r--r--test/irb/test_context.rb7
6 files changed, 42 insertions, 30 deletions
@@ -1463,16 +1463,21 @@ module IRB
end
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 = @context.main.to_s rescue "!#{$!.class}"
truncate_prompt_main(main_str)
when "M"
- main_str = @context.main.inspect rescue "!#{$!.class}"
truncate_prompt_main(main_str)
when "l"
ltype
@@ -19,7 +19,7 @@ module IRB
# Use throw and catch to handle arg that includes `;`
# For example: "1, kw: (2; 3); 4" will be parsed to [[1], { kw: 3 }]
catch(:EXTRACT_RUBY_ARGS) do
- @irb_context.workspace.binding.eval "IRB::Command.extract_ruby_args #{arg}"
end || [[], {}]
end
end
@@ -156,7 +156,8 @@ module IRB
end
def eval_class_constants
- ::Module.instance_method(:constants).bind(eval("self.class")).call
end
end
}
@@ -4,8 +4,6 @@
# by Keiju ISHITSUKA([email protected])
#
-require "delegate"
-
require_relative "helper_method"
IRB::TOPLEVEL_BINDING = binding
@@ -16,7 +14,7 @@ module IRB # :nodoc:
# set self to main if specified, otherwise
# inherit main from TOPLEVEL_BINDING.
def initialize(*main)
- if main[0].kind_of?(Binding)
@binding = main.shift
elsif IRB.conf[:SINGLE_IRB]
@binding = TOPLEVEL_BINDING
@@ -70,37 +68,16 @@ EOF
unless main.empty?
case @main
when Module
- @binding = eval("IRB.conf[:__MAIN__].module_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
else
begin
- @binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, __FILE__, __LINE__)
rescue TypeError
fail CantChangeBinding, @main.inspect
end
end
end
- case @main
- when Object
- use_delegator = @main.frozen?
- else
- use_delegator = true
- end
-
- if use_delegator
- @main = SimpleDelegator.new(@main)
- IRB.conf[:__MAIN__] = @main
- @main.singleton_class.class_eval do
- private
- define_method(:binding, Kernel.instance_method(:binding))
- define_method(:local_variables, Kernel.instance_method(:local_variables))
- # Define empty method to avoid delegator warning, will be overridden.
- define_method(:exit) {|*a, &b| }
- define_method(:exit!) {|*a, &b| }
- end
- @binding = eval("IRB.conf[:__MAIN__].instance_eval('binding', __FILE__, __LINE__)", @binding, *@binding.source_location)
- end
-
@binding.local_variable_set(:_, nil)
end
@@ -111,6 +88,9 @@ EOF
attr_reader :main
def load_helper_methods_to_main
ancestors = class<<main;ancestors;end
main.extend ExtendCommandBundle if !ancestors.include?(ExtendCommandBundle)
main.extend HelpersContainer if !ancestors.include?(HelpersContainer)
@@ -19,6 +19,12 @@ module TestIRB
end
end
binding.irb
RUBY
end
@@ -40,6 +46,19 @@ module TestIRB
assert_match(/irb\(Foo\):006>/, out)
end
def test_cd_moves_top_level_with_no_args
out = run_ruby_file do
type "cd Foo"
@@ -652,6 +652,13 @@ module TestIRB
assert_equal('irb("aaaaaaaaaaaaaaaaaaaaaaaaaaaa...)>', irb.send(:format_prompt, 'irb(%M)>', nil, 1, 1))
end
def test_prompt_main_raise
main = Object.new
def main.to_s; raise TypeError; end