calf
calf

Reputation: 881

Trying to write a file with a variable in the file name

What I have:

(Date.new(2013, 01, 01)..Date.new(2013, 01, 03)).each do |date|

    #some code

    File.open('data-date.json', 'w') do |d|  
      d.puts JSON.pretty_generate(employee_final) 
    end

end

This generates a file named data-date.json. I need data-20130101.json, for example.

I tried with and without quotes around date and printf '%02d%02d%d', date.month, date.day, date.year instead of date.

Upvotes: 0

Views: 566

Answers (1)

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230306

Try this (note the double quotes around the string):

File.open("data-#{date.strftime('%Y%m%d')}.json", 'w') do |d| 

Upvotes: 2

Related Questions