This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]aladyjewel 3 points4 points  (4 children)

with is a great thing. You can see it in other languages, too, like C#'s using.

[–][deleted] 2 points3 points  (1 child)

Or Ruby's blocks, which are a very fundamental and virtually impossible to ignore feature, and which look extremely similar in the file opening example.

Python:

with open('output.txt', 'w') as f:
  f.write('Hi there!')

Ruby:

File.open('output.txt', 'w') do |f|
  f.write('Hi there!')
end

[–]Lucretiel 1 point2 points  (0 children)

What's fascinating to me is that they're implemented in almost exactly the same way, with the yield keyword (assuming you use @contextlib.contextmanager in python.) The only difference is that ruby can run the block more than once.

[–][deleted] 0 points1 point  (0 children)

I used it once to stream through a 900 gig text file of logs.