# Problem writibg to temporary files

**URL:** <https://www.ruby-forum.com/t/problem-writibg-to-temporary-files/116848>\
**Category:** Ruby\
**Created:** [October 10, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/problem-writibg-to-temporary-files/116848 "2007-10-10T12:01:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2007, 12:01pm UTC](https://www.ruby-forum.com/t/problem-writibg-to-temporary-files/116848/1 "2007-10-10T12:01:54Z")

</div>

Hi folks,

```
       I have been facing problem while writing data to a temporary

```

file.  
I have the following code on my ubuntu machine:

```
 1. encrypted_content = @crypto_key.encrypt(attachment.read)
 2. tmpFile = Tempfile.new("encrypted_file")
 3. tmpFile.write("#{encrypted_content}")
 4. # i tried this too tmpFile.write(encrypted_content)
 5. tmpFile.flush

```

After executing this code, I could only find an empty temporary file  
under tmp/ directory in my file system. Why didn’t my code write the  
content into my tmpFile?

when I replaced line 3 with the following: tmpFile.write(“hello world  
!”), I could find the “hello world !” in my tmpFile. What could be the  
reason? Did I go wrong any where? Any help appreciated

regards,  
Venkat.

---

<div class="post-metadata">

**Author:** ![learningruby](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/6bbea6/32.png) [@learningruby](https://www.ruby-forum.com/u/learningruby)\
**Post date:** [October 10, 2007, 2:25pm UTC](https://www.ruby-forum.com/t/problem-writibg-to-temporary-files/116848/2 "2007-10-10T14:25:59Z")

</div>

On Oct 10, 12:02 pm, Venkat B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> 5. tmpFile.flush
> 
> ```
> 
> ## Venkat.
> 
> Posted viahttp://www.ruby-forum.com/.

I have tried you code with just a string to store and it works fine.  
I added a tmpFile.close to play nice.

> From my perspective it looks like that  
> @crypto\_key.encrypt(attachment.read) returns some object with empty  
> string as a result of to\_s message or nil.  
> Generally, I will start from there to find what is going on.
