Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid using arbitrary puts to print output #1025

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lib/irb.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
12 changes: 11 additions & 1 deletion lib/irb/command/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
40 changes: 40 additions & 0 deletions lib/irb/input-method.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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.
#
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
#
Expand Down Expand Up @@ -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.
#
Expand Down
8 changes: 8 additions & 0 deletions test/irb/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading