# Erasing text from a file

**URL:** <https://www.ruby-forum.com/t/erasing-text-from-a-file/137289>\
**Category:** Ruby\
**Created:** [May 11, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289 "2008-05-11T12:56:48Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 12:56pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/1 "2008-05-11T12:56:48Z")

</div>

To me it seems it should be a rather obvious thing, but no matter where  
I look, I can’t find how to erase text from a file or clear a file. The  
only things I have found are moving the the cursor to the start and  
writing more text over it (but that doesn’t get rid of it), or reopening  
the file.

Is there a way to simple erase text from the file?

Thanks,  
Lucas

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 1:21pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/2 "2008-05-11T13:21:36Z")

</div>

Lucas L. ha scritto:

> 

File.open(‘file.txt’, ‘w’) { |file| file = nil }

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 2:14pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/3 "2008-05-11T14:14:30Z")

</div>

Andrea F. wrote:

> File.open(‘file.txt’, ‘w’) { |file| file = nil }

That reopens the file. Is there a way to do it without reopening?

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 2:36pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/4 "2008-05-11T14:36:18Z")

</div>

Lucas L. wrote:

> Andrea F. wrote:
> 
> > File.open(‘file.txt’, ‘w’) { |file| file = nil }

The “file = nil” part is completely unneccessary. You can just do  
File.open(‘file.txt’, ‘w’) {}

> Is there a way to do it without reopening?

I don’t think there is.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 3:30pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/5 "2008-05-11T15:30:26Z")

</div>

Lucas L. wrote:

> Is there a way to simple erase text from the file?

perhaps File#truncate

## ---------------------------------------------------------- File#truncate file.truncate(integer) =\> 0

```
 Truncates _file_ to at most _integer_ bytes. The file must be
 opened for writing. Not available on all platforms.

    f = File.new("out", "w")
    f.syswrite("1234567890") #=> 10
    f.truncate(5) #=> 0
    f.close() #=> nil
    File.size("out") #=> 5

```

Guy Decoux

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 11, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/6 "2008-05-11T15:19:18Z")

</div>

Sebastian H. ha scritto:

> 

Ok, thank you 🙂

Andrea

---

<div class="post-metadata">

**Author:** ![genericguy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/5daacb/32.png) [@genericguy](https://www.ruby-forum.com/u/genericguy)\
**Post date:** [May 12, 2008, 3:19am UTC](https://www.ruby-forum.com/t/erasing-text-from-a-file/137289/7 "2008-05-12T03:19:56Z")

</div>

ts wrote:

> perhaps File#truncate

The documentation says this is platform specific, which I’d like to  
avoid.  
File.open will have to do.
