From: Doug Jolley on
I use Net::HTTP to collect some data as a string. I now need to pass
that string data to a Ruby method that is expecting to receive the data
from a file (i.e., the method expects the data to be stored in a file
and to have a path to the file passed to it as a parameter). Is there
anyway to resolve this dilemma short of writing the string data to a
file and then reading it in from the file?

Thanks for any input.

... doug
--
Posted via http://www.ruby-forum.com/.

From: Ryan Davis on

On Jun 28, 2010, at 17:43 , Doug Jolley wrote:

> I use Net::HTTP to collect some data as a string. I now need to pass
> that string data to a Ruby method that is expecting to receive the data
> from a file (i.e., the method expects the data to be stored in a file
> and to have a path to the file passed to it as a parameter). Is there
> anyway to resolve this dilemma short of writing the string data to a
> file and then reading it in from the file?

ri StringIO



From: Brian Candler on
Ryan Davis wrote:
> ri StringIO

That will work if the code in question will accept an open File/IO
object as an argument.

If it takes only a pathname argument, then you're stuck with writing the
data to a file (ri Tempfile may help).

If you have control of the target code, then refactor it. e.g.

class Foo
# original entry point
def read_file(pathname)
File.open(pathname,"rb") { |f| read_io(f) }
end

# entry point for already-open object, e.g. STDIN, a StringIO etc.
def read_io(io)
io.each_line { ... }
end
end
--
Posted via http://www.ruby-forum.com/.

From: Doug Jolley on
> If it takes only a pathname argument, then you're
> stuck with writing the data to a file

Unfortunately that is precisely my case and that is precisely what I was
trying to avoid. (And, unfortunately, I don't have any control over the
target code.)

Interestingly, a post that I found seemed to say that I could use the
StringIO approach in the case where a pathname argument was required.
The post said:

> Any easy way to work with a string in a method that is expecting
> a file is to create a new StringIO object and pass the result to
> the method requiring a file type. For example:

> some_method(StringIO.new("Your string here"))

He did say, "file". It's just that usually methods that follow that
form are expecting a path. Anyway, as one might expect, it didn't work
for me. I get the following error:

/test1:5:in `read': can't convert StringIO into String (TypeError)

As Ryan says, I guess that I'm stuck to write this out to a temp file.

Thanks to all who responded to my inquiry.

... doug
--
Posted via http://www.ruby-forum.com/.

From: Tony Arcieri on
[Note: parts of this message were removed to make it a legal post.]

On Tue, Jun 29, 2010 at 11:50 AM, Doug Jolley <ddjolley(a)gmail.com> wrote:

> Unfortunately that is precisely my case and that is precisely what I was
> trying to avoid. (And, unfortunately, I don't have any control over the
> target code.)


It's Ruby. You can always patch or alias_method_chain the target code if
you're willing to bear some slight brittleness.

--
Tony Arcieri
Medioh! A Kudelski Brand

 |  Next  |  Last
Pages: 1 2 3 4 5
Prev: Trying to reach Florian Frank
Next: can't use uki