Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

File.open with block vs without

Tags:

ruby

I have a question about Block, do the two codes mean the same?

code 1

File::open('yozloy.txt','w') do |f|   f << 'Some contains' end 

code 2

newFile = File::open('yozloy.txt','w') newFile << 'Some contains' 
like image 674
mko Avatar asked Nov 19 '10 11:11

mko


People also ask

What is the difference between open and with open in python?

Part 1: The Difference Between open and with open Basically, using with just ensures that you don't forget to close() the file, making it safer/preventing memory issues.

Why do we use with open?

It eliminates the need to write your own finally blocks, and it structures your code such that you avoid duplicative if statements, and it allows readers to easily locate the place where a file object (or rather variable holding a file object) is defined.

How do I open a block file?

If you cannot open your BLOCK file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a BLOCK file directly in the browser . Just drag the file onto this browser window and drop it.

How do I open a file in non-blocking mode in Linux?

As I mentioned earlier, a file can be opened in non-blocking mode with the open () system call. You do this by OR-ing O_NONBLOCK with the rest of the file flags used in the open () call, such as such as O_RDONLY or O_RDWR.

What is the block file extension?

The BLOCK file type is primarily associated with Apple II operating system. BLOCK files mostly belong to Apple II operating system. Use our " Online BLOCK Text Viewer " below to analyze your BLOCK file and to see all text it contains.

How do I open a non-blocking file descriptor?

As we’ve already mentioned, file descriptors are, by default, blocking. You can open a file descriptor as non-blocking by adding a flag to the open (), and you can change a file descriptor between blocking and non-blocking via the fcntl () call.


1 Answers

No, they do not mean the same. In the first example, the file is automatically closed after the block was processed. In the second example, it's your responsibility to manually call newFile.close.

like image 174
DarkDust Avatar answered Sep 28 '22 17:09

DarkDust