From e7ae7b30eb467fdaca8f73f6b7bc4affc5722c59 Mon Sep 17 00:00:00 2001 From: qwyng Date: Sat, 2 Nov 2024 11:45:16 +0900 Subject: [PATCH] Avoid using arbitrary puts to print output --- lib/irb.rb | 8 ++++++++ lib/irb/command/base.rb | 12 +++++++++++- lib/irb/input-method.rb | 40 ++++++++++++++++++++++++++++++++++++++++ test/irb/helper.rb | 8 ++++++++ 4 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/irb.rb b/lib/irb.rb index 213e23117..dc6de1bb1 100644 --- a/lib/irb.rb +++ b/lib/irb.rb @@ -1422,6 +1422,14 @@ def inspect private + def puts(...) + @context.io.puts(...) + end + + def print(...) + @context.io.print(...) + end + def generate_prompt(opens, continue, line_offset) ltype = @scanner.ltype_from_open_tokens(opens) indent = @scanner.calc_indent_level(opens) diff --git a/lib/irb/command/base.rb b/lib/irb/command/base.rb index af810ed34..4862f980f 100644 --- a/lib/irb/command/base.rb +++ b/lib/irb/command/base.rb @@ -36,7 +36,7 @@ def help_message(help_message = nil) def execute(irb_context, arg) new(irb_context).execute(arg) rescue CommandArgumentError => e - puts e.message + irb_context.io.puts e.message end private @@ -55,6 +55,16 @@ def initialize(irb_context) def execute(arg) #nop end + + private + + def puts(...) + irb_context.io.puts(...) + end + + def print(...) + irb_context.io.print(...) + end end Nop = Base diff --git a/lib/irb/input-method.rb b/lib/irb/input-method.rb index 210d3da78..a3fe41f91 100644 --- a/lib/irb/input-method.rb +++ b/lib/irb/input-method.rb @@ -48,6 +48,14 @@ def prompting? false end + def puts + fail NotImplementedError + end + + def print + fail NotImplementedError + end + # For debug message def inspect 'Abstract InputMethod' @@ -73,6 +81,14 @@ def gets @line[@line_no += 1] = line end + def puts(...) + @stdout.puts(...) + end + + def print(...) + @stdout.print(...) + end + # Whether the end of this input method has been reached, returns +true+ if # there is no more data to read. # @@ -155,6 +171,14 @@ def gets @io.gets end + def puts(...) + ::Kernel.puts(...) + end + + def print(...) + ::Kernel.print(...) + end + # The external encoding for standard input. def encoding @external_encoding @@ -223,6 +247,14 @@ def gets end end + def puts(...) + @stdout.puts(...) + end + + def print(...) + @stdout.format(...) + end + # Whether the end of this input method has been reached, returns +true+ # if there is no more data to read. # @@ -474,6 +506,14 @@ def gets end end + def puts(...) + @stdout.puts(...) + end + + def print(...) + @stdout.print(...) + end + # Whether the end of this input method has been reached, returns +true+ # if there is no more data to read. # diff --git a/test/irb/helper.rb b/test/irb/helper.rb index ea2c6ef16..1659b452f 100644 --- a/test/irb/helper.rb +++ b/test/irb/helper.rb @@ -32,6 +32,14 @@ def gets @list[@line_no]&.tap {@line_no += 1} end + def puts(...) + Kernel.puts(...) + end + + def print(...) + Kernel.print(...) + end + def eof? @line_no >= @list.size end