The write
command, as you used it, would write the return value of pi
to the file. The returned value from pi
is not a string of digits, but the last value calculated inside the routine (which happens to always be the last assignment to r
)
The closest fix (i.e. the least amount you need to change to get it to work) is to output the yielded digits to the file, which you are already doing with printing to stdout:
File.open("pi.txt", 'w') do |pitxt|
pi do |digit|
print digit
$stdout.flush
pitxt.write digit
end
end
This should at least get you the digits you need into the text file.
You have several more mistakes in your script. I suggest you take things more slowly (in fact I recommend you remove the image-writing code as it is, and have another go!) Feel free to ask more questions on SO as you progress. For your next question, instead of presenting the entire script and asking "what's wrong with this", try to write a simple isolated example that demonstrates what you are trying to do, and has a single thing that you cannot get to work. For example, now you already have the digits of pi correct, there is no need to ask about that code - the image-writing part should be able to cope with any string of digits. You may even find that putting examples together allows you to answer your own question.