# FasterCSV - Writing to File Question

**URL:** <https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294>\
**Category:** Ruby\
**Created:** [December 13, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294 "2006-12-13T23:02:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [December 13, 2006, 11:02pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294/1 "2006-12-13T23:02:56Z")

</div>

Based on a previous thread, I gave FasterCSV a try and I’ve been very  
impressed with the performance increases. However, something about the  
syntax for writing to a file bothers me. Is there any way to create a  
“writer object” to write to a file? For example, this is how I’m  
currently using FasterCSV:

FCSV.open(my\_file,“w”) do |out|  
out \<\< my\_data  
…  
end

What I’d like to do is something along the lines of:

out = FCSV.open(my\_file,“w”)  
out \<\< my\_data  
…  
out.close

Is this functionality present? If not, is this a design decision? I  
guess it just bothers me to have to wrap a block around all my code that  
has to do with writing to a file.

Thanks in advance,  
Drew

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [December 13, 2006, 11:14pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294/2 "2006-12-13T23:14:39Z")

</div>

Drew O. wrote:  
snip

> Thanks in advance,  
> Drew

Well, irb is my friend and I should have used it before asking.  
FasterCSV indeed does have this functionality and it works as expected.  
I had an error with this functionality before posting but it must have  
been my mistake.

Thanks,  
Drew

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [December 13, 2006, 11:20pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294/3 "2006-12-13T23:20:02Z")

</div>

On Dec 13, 2006, at 4:02 PM, Drew O. wrote:

> What I’d like to do is something along the lines of:
> 
> out = FCSV.open(my\_file,“w”)  
> out \<\< my\_data  
> …  
> out.close
> 
> Is this functionality present?

Sure. Should work just fine. (File a bug if it doesn’t!)

Just FYI though, the first example is very idiomatic Ruby. We would  
always rather let the language clean up after us than to have to  
remember to do it ourselves. That’s why you see this construct all  
over Ruby even with normal File writes.

James Edward G. II

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [December 13, 2006, 11:23pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294/4 "2006-12-13T23:23:14Z")

</div>

James G. wrote:

> On Dec 13, 2006, at 4:02 PM, Drew O. wrote:
> 
> > What I’d like to do is something along the lines of:
> > 
> > out = FCSV.open(my\_file,“w”)  
> > out \<\< my\_data  
> > …  
> > out.close
> > 
> > Is this functionality present?
> 
> Sure. Should work just fine. (File a bug if it doesn’t!)
> 
> Just FYI though, the first example is very idiomatic Ruby. We would  
> always rather let the language clean up after us than to have to  
> remember to do it ourselves. That’s why you see this construct all  
> over Ruby even with normal File writes.
> 
> James Edward G. II

James -

Was hoping you’d chime in. Thanks for the explanation, this is what I  
expected. I supposed I just have the beat the Java that’s remaining in  
me by getting used to this type of code. I can certainly see the benefit  
of clean up being done for you.

Thanks,  
Drew

---

<div class="post-metadata">

**Author:** ![dfg59](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/e0b2c6/32.png) [@dfg59](https://www.ruby-forum.com/u/dfg59)\
**Post date:** [December 13, 2006, 11:22pm UTC](https://www.ruby-forum.com/t/fastercsv-writing-to-file-question/79294/5 "2006-12-13T23:22:02Z")

</div>

Drew O. wrote:

> Thanks in advance,  
> Drew
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

I don’t use FCSV but I’d be wouldn’t be surprised if it didn’t have a  
close method (although it may - just have a look at the code.) The  
pattern of associating a block with the opening/initialization of a  
resource such as a file, connection, etc. is one of Ruby’s major  
strengths and if the designer of the entity being opened has done their  
job right, the entity is (usually) closed for you when the block  
terminates. This pattern leads to fewer defects such as leaked file  
handles and such. So, I suppose at the end of the day this is a matter  
of personal taste but I think most Ruby enthusiasts would prefer the  
block to the explicit close. And I do believe this is an example of  
the “initialization is acquisition” design pattern, if memory serves  
me, so its not as if this is some arcane practice, for what ever my 2  
cents are worth 🙂

Ken
