# How to Create a New File

**URL:** <https://www.ruby-forum.com/t/how-to-create-a-new-file/100564>\
**Category:** Ruby\
**Created:** [July 30, 2006, 7:36am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564 "2006-07-30T07:36:53Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 7:36am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/1 "2006-07-30T07:36:53Z")

</div>

let’s say I want to create a new file named new\_file.txt. how do i do  
this in ruby? it would seem to be trivial, however, i’m going through  
the pickaxe book and i can’t find anything that relates to this.

tia…

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 7:46am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/2 "2006-07-30T07:46:03Z")

</div>

How about something like this:

File.open( ‘new\_file.txt’, ‘w’ ) {|file| file.write( ‘yo’ )}

#hth,

# -Harold

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 7:46am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/3 "2006-07-30T07:46:13Z")

</div>

Different ways:

Easy pure ruby:

f = File.open(“new\_file.txt”,“w”)  
f.close

Or if you are on \*nix/cygwin:

system(“touch new\_file.txt”)

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 8:31am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/4 "2006-07-30T08:31:56Z")

</div>

This is just golfing, and I should be ashamed of myself, but  
something like …

```
`>~/new_file.txt`

```

… will do it on most Unix systems (including Mac OS X).

Regards, Morton

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 8:47am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/5 "2006-07-30T08:47:28Z")

</div>

On Sun, Jul 30, 2006 at 03:31:30PM +0900, Morton G. wrote:

> This is just golfing, and I should be ashamed of myself, but  
> something like …
> 
> `>~/new_file.txt`

Whee, CLI obfu. Well, something potentially useful for it, anyway.

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 8:47am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/6 "2006-07-30T08:47:18Z")

</div>

thanks, everyone.

Harold’s solution works. i tried this before posting, but got an  
error. i guess i mucked up the code, b/c it works fine now.

thanks again.

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 8:53am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/7 "2006-07-30T08:53:39Z")

</div>

Skeets wrote:

> let’s say I want to create a new file named new\_file.txt. how do i do  
> this in ruby? it would seem to be trivial, however, i’m going through  
> the pickaxe book and i can’t find anything that relates to this.
> 
> tia…

i know why my initial attempt was mucked up…

f = File.open( ‘new\_file.txt’, ‘w’ )

i tried to assign it to a variable - that’s a no no in this case.

---

<div class="post-metadata">

**Author:** ![Skeets](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/8e8cbc/32.png) [@Skeets](https://www.ruby-forum.com/u/Skeets)\
**Post date:** [July 30, 2006, 9:12am UTC](https://www.ruby-forum.com/t/how-to-create-a-new-file/100564/8 "2006-07-30T09:12:00Z")

</div>

Skeets wrote:

> i know why my initial attempt was mucked up…
> 
> f = File.open( ‘new\_file.txt’, ‘w’ )
> 
> i tried to assign it to a variable - that’s a no no in this case.

Um, no… that’s not a nono. 🙂

What did this do for you?

Of course, properly you’d use File.new in that case,  
and if you use File.open you’d use a block…

Hal
