Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8957855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T15:05:16+00:00 2026-06-15T15:05:16+00:00

When using Tempfile Ruby is creating a file with a thread-safe and inter-process-safe name.

  • 0

When using Tempfile Ruby is creating a file with a thread-safe and inter-process-safe name. I only need a file name in that way.

I was wondering if there is a more straight forward approach way than:

t = Tempfile.new(['fleischwurst', '.png'])
temp_path = t.path
t.close
t.unlink
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T15:05:17+00:00Added an answer on June 15, 2026 at 3:05 pm

    Dir::Tmpname.create

    You could use Dir::Tmpname.create. It figures out what temporary directory to use (unless you pass it a directory). It’s a little ugly to use given that it expects a block:

    require 'tmpdir'
    # => true
    Dir::Tmpname.create(['prefix-', '.ext']) {}
    # => "/tmp/prefix-20190827-1-87n9iu.ext"
    Dir::Tmpname.create(['prefix-', '.ext'], '/my/custom/directory') {}
    # => "/my/custom/directory/prefix-20190827-1-11x2u0h.ext"
    

    The block is there for code to test if the file exists and raise an Errno::EEXIST so that a new name can be generated with incrementing value appended on the end.

    The Rails Solution

    The solution implemented by Ruby on Rails is short and similar to the solution originally implemented in Ruby:

    require 'tmpdir'
    # => true
    File.join(Dir.tmpdir, "YOUR_PREFIX-#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}-YOUR_SUFFIX")
    => "/tmp/YOUR_PREFIX-20190827-1-wyouwg-YOUR_SUFFIX"
    File.join(Dir.tmpdir, "YOUR_PREFIX-#{Time.now.strftime("%Y%m%d")}-#{$$}-#{rand(0x100000000).to_s(36)}-YOUR_SUFFIX")
    => "/tmp/YOUR_PREFIX-20190827-1-140far-YOUR_SUFFIX"
    

    Dir::Tmpname.make_tmpname (Ruby 2.5.0 and earlier)

    Dir::Tmpname.make_tmpname was removed in Ruby 2.5.0. Prior to Ruby 2.4.4 it could accept a directory path as a prefix, but as of Ruby 2.4.4, directory separators are removed.

    Digging in tempfile.rb you’ll notice that Tempfile includes Dir::Tmpname. Inside you’ll find make_tmpname which does what you ask for.

    require 'tmpdir'
    # => true
    File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname("prefix-", nil))
    # => "/tmp/prefix-20190827-1-dfhvld"
    File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(["prefix-", ".ext"], nil))
    # => "/tmp/prefix-20190827-1-19zjck1.ext"
    File.join(Dir.tmpdir, Dir::Tmpname.make_tmpname(["prefix-", ".ext"], "suffix"))
    # => "/tmp/prefix-20190827-1-f5ipo7-suffix.ext"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am creating a tempfile using C#. public partial class frmResults : Form {
In Python I open a temporary file for write by using tempfile.mkstemp in order
I'm creating a collage of thumbnails using Ruby 1.9.3. The thumbnails are being loaded
We're using a third party web service that POSTS multipart data (a file) back
I'm using System.Diagnostics.Process class to convert wav file to mp3 file in a separated
I was wondering how to implement an image upload using ruby on rails v3?
Using a CSS image sprite, I'm creating an 'interactive' image where hovering over certain
Using SQL Server 2008 R2 we are looking for a way to select the
Using ASIHTTPRequest, I downloaded a zip file containing a folder with several audio files.
I need to generate a pdf from a tex file, as a temporary file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.