# File.new variable with extension

**URL:** <https://www.ruby-forum.com/t/file-new-variable-with-extension/137881>\
**Category:** Rails\
**Created:** [May 17, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/file-new-variable-with-extension/137881 "2008-05-17T15:38:12Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [May 17, 2008, 3:38pm UTC](https://www.ruby-forum.com/t/file-new-variable-with-extension/137881/1 "2008-05-17T15:38:12Z")

</div>

I ned to create a file of the name of a variable?  
but i can’t create an extension.

File.new((@page.title), ‘w’ ) {|file| file.write( ‘testfile’ )}

how can i maka a extension like this

File.new((@page.title.txt), ‘w’ ) {|file| file.write( ‘testfile’ )}

---

<div class="post-metadata">

**Author:** ![gorter9](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/839c29/32.png) [@gorter9](https://www.ruby-forum.com/u/gorter9)\
**Post date:** [May 17, 2008, 8:24pm UTC](https://www.ruby-forum.com/t/file-new-variable-with-extension/137881/2 "2008-05-17T20:24:41Z")

</div>

What you want to do is:

File.new(@page.title + ‘.txt’, ‘w’ ) {|file| file \<\< ‘content’ }

Above the “file \<\< ‘content’” makes sure that the file pointer is  
closed after the content is written instead of leaving it open as in  
your example “file.write( ‘testfile’ )”.

–  
Thiago J.  
[http://www.railsfreaks.com](http://www.railsfreaks.com)

On May 17, 6:38 am, GA Gorter [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
