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

Make OStream#write accept multiple arguments #206

Merged
merged 5 commits into from
Aug 24, 2019
Merged
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
32 changes: 22 additions & 10 deletions lib/iruby/ostream.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,38 @@ def read(*args)
alias_method :next, :read
alias_method :readline, :read

def write(s)
raise 'I/O operation on closed file' unless @session
@session.send(:publish, :stream, name: @name, text: s.to_s)
nil
def write(*obj)
str = build_string { |sio| sio.write(*obj) }
session_send(str)
end
alias_method :<<, :write
alias_method :print, :write

def printf(*fmt)
write sprintf(*fmt)
def printf(format, *obj)
str = build_string { |sio| sio.printf(format, *obj) }
session_send(str)
end

def puts(*lines)
lines = [''] if lines.empty?
lines.each { |s| write("#{s}\n")}
nil
def puts(*obj)
str = build_string { |sio| sio.puts(*obj) }
session_send(str)
end

def writelines(lines)
lines.each { |s| write(s) }
end

private

def build_string
StringIO.open { |sio| yield(sio); sio.string }
end

def session_send(str)
raise 'I/O operation on closed file' unless @session

@session.send(:publish, :stream, name: @name, text: str)
nil
end
end
end