RosettaCodeData/Task/Write-float-arrays-to-a-text-file/Ruby/write-float-arrays-to-a-text-file.rb
2023-07-01 13:44:08 -04:00

13 lines
307 B
Ruby

# prepare test data
x = [1, 2, 3, 1e11]
y = x.collect { |xx| Math.sqrt xx }
xprecision = 3
yprecision = 5
# write the arrays
open('sqrt.dat', 'w') do |f|
x.zip(y) { |xx, yy| f.printf("%.*g\t%.*g\n", xprecision, xx, yprecision, yy) }
end
# print the result file
open('sqrt.dat', 'r') { |f| puts f.read }