Drew Olson wrote:
> 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
>
> --
> Posted via 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