< Previous
Dichotomous Google Memewatch >

[Comments] (3) : Floyd from The Fresh Loaf, who presumably knows from bread and bread byproducts, tells me that the sourdough soup I had at the Polish restaurant was probably zurek, and looking at the recipe I agree. Zurek is made with rye sourdough starter, so it is actually sourdough soup. Inventive.

Floyd also mentioned that he dabbles in Ruby, so that's one. I am going through the Poignant Guide, which is funny and gonzo and Fafbloggish, and I have a simple question about idiom. I see code like the following, which sets up a block that deals with a single item:

File::open('foo.txt', 'w') do |f|
  f << "text"
end

This idiom makes sense when you're iterating over a bunch of items, but why create a new block to deal with a single item? Is it somehow more Rubic (Rubyic?) than the following code, which seems much more straightforward?:

f = File::open('foo.txt', 'w')
f << "text"

Let me know. I'm trying to spare you the details of my mastery of Ruby (and puppets), but this is undoubtedly the first of many questions.

Filed under: ,

Comments:

Posted by Sean Neakums at Wed Jun 22 2005 03:54

The first way avoids polluting the enclosing scope with f, or so I assume. I guess they like that in Ruby-land.

Posted by Adam G at Wed Jun 22 2005 08:30

The first way ensures that the file gets closed when the block exits, no matter how the block exits, even if whatever you do inside the block raises an exception or throws something.

Posted by Leonard at Wed Jun 22 2005 11:13

Okay, that makes sense. I would expect the file to get closed whenever the variable went out of scope, even when 'going out of scope' meant leaving the program, but it doesn't hurt to make it explicit.


[Main]

Unless otherwise noted, all content licensed by Leonard Richardson
under a Creative Commons License.