From: Kevin Smith Date: 2000-12-20T11:53:50+09:00 Subject: [ruby-talk:7732] Re: String as IO and/or File object? Robert Feldt wrote: >Is there a simple way to access a string as an IO and/or File object? If >not have you seen any code for doing it? I'll take a stab at this. My background is in static languages like C++, so hopefully I'll get it right. What does it mean to access a string as an IO? I think it just means that you want to have an object that could be a string, or could be an IO, and treat it the same for both cases. In a static language, you'd have to use an interface (Java) or a common parent class (C++). In Ruby, both the string and IO classes support a few of the same methods: <<, each, each_byte, each_line. If you restrict your access to just these methods, then you can act on a variable that contains a string or an IO and not know the difference. Maybe you want to do something not on this list, like...um...puts. In that case, I might create a class IOString that inherits from String. I would then define an IOString#puts method that does the right thing (probably appends the passed parameter and a newline). If that doesn't answer your question, please indicate what methods of IO you would like to apply to a string. Kevin