Skip to content

Commit 838c1bc

Browse files
committed
Refactor output_data and output_plot for Ruby 2.7 Tempfile compatibility
1 parent f37ba8c commit 838c1bc

1 file changed

Lines changed: 27 additions & 13 deletions

File tree

‎lib/youplot/command.rb‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -178,26 +178,40 @@ def create_plot
178178

179179
def output_data(input)
180180
# Pass the input to subsequent pipelines
181-
case options[:pass]
182-
when IO, StringIO
183-
options[:pass].print(input)
184-
else
185-
if options[:pass]
186-
File.open(options[:pass], 'w') do |f|
187-
f.print(input)
188-
end
181+
out = options[:pass]
182+
# Handle Tempfile first to keep tests and behavior consistent.
183+
# Ruby 2.7 Tempfile is Delegator-based and does not match IO/File checks.
184+
# Then handle path strings and IO-like objects.
185+
case out
186+
when Tempfile
187+
File.open(out.path, 'w') do |f|
188+
f.print(input)
189189
end
190+
when String
191+
File.open(out, 'w') do |f|
192+
f.print(input)
193+
end
194+
else
195+
out.print(input) if out.respond_to?(:print)
190196
end
191197
end
192198

193199
def output_plot(plot)
194-
case options[:output]
195-
when IO, StringIO
196-
plot.render(options[:output])
197-
when String, Tempfile
198-
File.open(options[:output], 'w') do |f|
200+
out = options[:output]
201+
# Handle Tempfile first to keep tests and behavior consistent.
202+
# Ruby 2.7 Tempfile is Delegator-based and does not match IO/File checks.
203+
# Then handle path strings and IO-like objects.
204+
case out
205+
when Tempfile
206+
File.open(out.path, 'w') do |f|
199207
plot.render(f)
200208
end
209+
when String
210+
File.open(out, 'w') do |f|
211+
plot.render(f)
212+
end
213+
else
214+
plot.render(out) if out.respond_to?(:write)
201215
end
202216
end
203217

0 commit comments

Comments
 (0)