What is the difference between these two methods?




they both open a file with modes but where is the difference ?

how can i check if the file closed or still open in crystal ?

1 Like

File.open is essentially just an alias for File.new, but provides a more readable API. File is an IO type so you should be able to use the #closed? method to determine if the file has been closed. Tho depending on what you’re doing, it may be best to use the block version of File.open such that it’ll close itself after the block ends.


now i get the point, i really thanks you so basically file.open() has the ability to close files after been written to while file.new not cannot close file after been written to automatically it needs to be closed manually right ?

Not quite. File.open has two variants. One that yields a block that automatically closes the file after the block, and a non-block version that works the same as File.new in which the file would need to be closed manually.

the same thing bro but thanks very much brother

what about Array() is it a function or a keyword what is it ? i know how to use it but just don’t know if it is a keyword or a function or i don’t know

Array is a type, checkout Array - Crystal and Array(T) - Crystal 1.10.0.

I consider new and open should be consider different as it names, one can handle new created, but another can only be open, can’t create a new one.

Sure this probably not true for File.open and File.new, but, we can use this policy to make code more clearly.

3 posts were split to a new topic: Specify type of number literal