StringIO
In some ways, the distinction between strings and file contents is artificial: the contents of a file are basically a string that happens to live on disk, not in memory. The StringIO class, available by using require "stringio", aims to unify the two concepts, making strings act as if they were opened IO objects. Once a string is wrapped in a StringIO object, it can be read from and written to as if it were an open file. This can make unit testing a lot easier.
The StringIO class isn’t a subclass of IO, it just implements many of the same read/write methods. Using StringIO lets you pass strings into classes and methods that were originally written to work with files. StringIO objects take their encoding from the string you pass in—if ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access