From: Brian Candler on
Robert Klemme wrote:
> At least one can use Tempfile for this, e.g.
>
> Tempfile "prefix", "/tmp" do |io|
> io.write everything
>
> io.seek 0
> whatever_load_routine io
> end

or rather:

Tempfile.open "prefix", "/tmp" do |io|
io.write everything
io.flush
whatever_load_routine io.path
end
--
Posted via http://www.ruby-forum.com/.

From: James Edward Gray II on
On Jun 30, 2010, at 7:53 AM, Brian Candler wrote:

> Robert Klemme wrote:
>> This is an interesting point of interface design: usually it is more
>> convenient to just pass a file name somewhere and that method opens
>> the file (or URL) and reads the data. But from a modularity point of
>> view it is generally better to pass an open IO like instance.
>
> Definitely. The original csv.rb in ruby 1.8 got this very badly wrong.
>
> The new (faster_csv) interface is capable of this, but it suffers from
> missing documentation.

I agree that FasterCSV's documentation isn't perfect. I'm pretty sure all of its functions are documented, but you would need to read the API like a novel to find them. I've been trying more tutorial style documentation lately, but there again it's hard to reference what you specifically want to know.

I'm open to suggestions and I do take patches.

> IIRR you have to do something like
>
> FasterCSV.new($stdin).each do |row|
> p row
> end

That works, yes.

> Since the documented "primary" interface is
> FasterCSV.foreach("path/to/file.csv"), you have to dig through the code
> to work out how to handle an open stream.

That's mostly due to a pet peeve of mine. I often see code that slurps when foreach() would have worked fine. That's why I try to push that as a first choice.

Do you think it would help if I added Wrapping an IO under the Shortcut Interface on this page?

http://fastercsv.rubyforge.org/classes/FasterCSV.html

James Edward Gray II


From: Robert Klemme on
2010/6/30 Brian Candler <b.candler(a)pobox.com>:
> Robert Klemme wrote:
>> At least one can use Tempfile for this, e.g.
>>
>> Tempfile "prefix", "/tmp" do |io|
>>   io.write everything
>>
>>   io.seek 0
>>   whatever_load_routine io
>> end
>
> or rather:
>
> Tempfile.open "prefix", "/tmp" do |io|
>  io.write everything
>  io.flush

I'd rather io.close instead of io.flush to release resources as soon
as possible.

>  whatever_load_routine io.path
> end

Ooops! Yes, of course. I copied the wrong example. Sorry for my confusion.

Cheers

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

From: Brian Candler on
Robert Klemme wrote:
> 2010/6/30 Brian Candler <b.candler(a)pobox.com>:
>> or rather:
>>
>> Tempfile.open "prefix", "/tmp" do |io|
>> �io.write everything
>> �io.flush
>
> I'd rather io.close instead of io.flush to release resources as soon
> as possible.

But tempfile will want to close itself using the block form anyway.

In most versions of ruby, Tempfile with a block returns nil. A change
was committed so that it returns the (closed) object, but that hasn't
made it into either of the versions I have lying around here.

>> tf = Tempfile.open("aaa","/tmp") { puts "hello"; 123 }
hello
=> nil
--
Posted via http://www.ruby-forum.com/.

From: Brian Candler on
James Edward Gray II wrote:
> I'm open to suggestions and I do take patches.

Specifically, I'd like to see how to parse CSV from stdin. You provide
an example in the opposite direction:

# FCSV($stderr) { |csv_err| csv_err << %w{my data here} } # to
$stderr

A bit more experimentation suggests that

FCSV($stdin).each { |a,b,c| p a,b,c }

works, so if that's a reasonable way to drive the library, I'd like to
see that mentioned under shortcuts. (I thought I'd tried that before and
it failed, but I must have done something different)
--
Posted via http://www.ruby-forum.com/.

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