This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.

Backing Up to Versioned Filenames
CodeExpectedActual
class File
  def File.versioned_filename(base, first_suffix='.0')
    suffix = nil
    filename = base
    while File.exists?(filename)
      suffix = (suffix ? suffix.succ : first_suffix)       
      filename = base + suffix
    end  
    return filename
  end
end
5.times do |i|
  name = File.versioned_filename('filename.txt')
  open(name, 'w') { |f| f << "Contents for run #{i}" }
  puts "Created #{name}"
end
Created filename.txt
Created filename.txt.0
Created filename.txt.1
Created filename.txt.2
Created filename.txt.3
Created filename.txt
Created filename.txt.0
Created filename.txt.1
Created filename.txt.2
Created filename.txt.3
require 'ftools'
class File
 def File.to_backup(filename, move=false)
    new_filename = nil
    if File.exists? filename
      new_filename = File.versioned_filename(filename)
      File.send(move ? :move : :copy, filename, new_filename)
    end
    return new_filename
  end
end
File.to_backup('filename.txt')
"filename.txt.4" "filename.txt.4"
File.to_backup('filename.txt')
"filename.txt.5" "filename.txt.5"
File.to_backup('filename.txt', true)
"filename.txt.6" "filename.txt.6"
File.exists? 'filename.txt'
false false
File.to_backup('filename.txt')
nil nil
200.times do |i|
  name = File.versioned_filename('many_versions.txt', '.000')
  open(name, 'w') { |f| f << "Contents for run #{i}" }
  puts "Created #{name}"
end
Created many_versions.txt
Created many_versions.txt.000
Created many_versions.txt.001
...
Created many_versions.txt.197
Created many_versions.txt.198
Created many_versions.txt
Created many_versions.txt.000
Created many_versions.txt.001
Created many_versions.txt.002
Created many_versions.txt.003
Created many_versions.txt.004
Created many_versions.txt.005
Created many_versions.txt.006
Created many_versions.txt.007
Created many_versions.txt.008
Created many_versions.txt.009
Created many_versions.txt.010
Created many_versions.txt.011
Created many_versions.txt.012
Created many_versions.txt.013
Created many_versions.txt.014
Created many_versions.txt.015
Created many_versions.txt.016
Created many_versions.txt.017
Created many_versions.txt.018
Created many_versions.txt.019
Created many_versions.txt.020
Created many_versions.txt.021
Created many_versions.txt.022
Created many_versions.txt.023
Created many_versions.txt.024
Created many_versions.txt.025
Created many_versions.txt.026
Created many_versions.txt.027
Created many_versions.txt.028
Created many_versions.txt.029
Created many_versions.txt.030
Created many_versions.txt.031
Created many_versions.txt.032
Created many_versions.txt.033
Created many_versions.txt.034
Created many_versions.txt.035
Created many_versions.txt.036
Created many_versions.txt.037
Created many_versions.txt.038
Created many_versions.txt.039
Created many_versions.txt.040
Created many_versions.txt.041
Created many_versions.txt.042
Created many_versions.txt.043
Created many_versions.txt.044
Created many_versions.txt.045
Created many_versions.txt.046
Created many_versions.txt.047
Created many_versions.txt.048
Created many_versions.txt.049
Created many_versions.txt.050
Created many_versions.txt.051
Created many_versions.txt.052
Created many_versions.txt.053
Created many_versions.txt.054
Created many_versions.txt.055
Created many_versions.txt.056
Created many_versions.txt.057
Created many_versions.txt.058
Created many_versions.txt.059
Created many_versions.txt.060
Created many_versions.txt.061
Created many_versions.txt.062
Created many_versions.txt.063
Created many_versions.txt.064
Created many_versions.txt.065
Created many_versions.txt.066
Created many_versions.txt.067
Created many_versions.txt.068
Created many_versions.txt.069
Created many_versions.txt.070
Created many_versions.txt.071
Created many_versions.txt.072
Created many_versions.txt.073
Created many_versions.txt.074
Created many_versions.txt.075
Created many_versions.txt.076
Created many_versions.txt.077
Created many_versions.txt.078
Created many_versions.txt.079
Created many_versions.txt.080
Created many_versions.txt.081
Created many_versions.txt.082
Created many_versions.txt.083
Created many_versions.txt.084
Created many_versions.txt.085
Created many_versions.txt.086
Created many_versions.txt.087
Created many_versions.txt.088
Created many_versions.txt.089
Created many_versions.txt.090
Created many_versions.txt.091
Created many_versions.txt.092
Created many_versions.txt.093
Created many_versions.txt.094
Created many_versions.txt.095
Created many_versions.txt.096
Created many_versions.txt.097
Created many_versions.txt.098
Created many_versions.txt.099
Created many_versions.txt.100
Created many_versions.txt.101
Created many_versions.txt.102
Created many_versions.txt.103
Created many_versions.txt.104
Created many_versions.txt.105
Created many_versions.txt.106
Created many_versions.txt.107
Created many_versions.txt.108
Created many_versions.txt.109
Created many_versions.txt.110
Created many_versions.txt.111
Created many_versions.txt.112
Created many_versions.txt.113
Created many_versions.txt.114
Created many_versions.txt.115
Created many_versions.txt.116
Created many_versions.txt.117
Created many_versions.txt.118
Created many_versions.txt.119
Created many_versions.txt.120
Created many_versions.txt.121
Created many_versions.txt.122
Created many_versions.txt.123
Created many_versions.txt.124
Created many_versions.txt.125
Created many_versions.txt.126
Created many_versions.txt.127
Created many_versions.txt.128
Created many_versions.txt.129
Created many_versions.txt.130
Created many_versions.txt.131
Created many_versions.txt.132
Created many_versions.txt.133
Created many_versions.txt.134
Created many_versions.txt.135
Created many_versions.txt.136
Created many_versions.txt.137
Created many_versions.txt.138
Created many_versions.txt.139
Created many_versions.txt.140
Created many_versions.txt.141
Created many_versions.txt.142
Created many_versions.txt.143
Created many_versions.txt.144
Created many_versions.txt.145
Created many_versions.txt.146
Created many_versions.txt.147
Created many_versions.txt.148
Created many_versions.txt.149
Created many_versions.txt.150
Created many_versions.txt.151
Created many_versions.txt.152
Created many_versions.txt.153
Created many_versions.txt.154
Created many_versions.txt.155
Created many_versions.txt.156
Created many_versions.txt.157
Created many_versions.txt.158
Created many_versions.txt.159
Created many_versions.txt.160
Created many_versions.txt.161
Created many_versions.txt.162
Created many_versions.txt.163
Created many_versions.txt.164
Created many_versions.txt.165
Created many_versions.txt.166
Created many_versions.txt.167
Created many_versions.txt.168
Created many_versions.txt.169
Created many_versions.txt.170
Created many_versions.txt.171
Created many_versions.txt.172
Created many_versions.txt.173
Created many_versions.txt.174
Created many_versions.txt.175
Created many_versions.txt.176
Created many_versions.txt.177
Created many_versions.txt.178
Created many_versions.txt.179
Created many_versions.txt.180
Created many_versions.txt.181
Created many_versions.txt.182
Created many_versions.txt.183
Created many_versions.txt.184
Created many_versions.txt.185
Created many_versions.txt.186
Created many_versions.txt.187
Created many_versions.txt.188
Created many_versions.txt.189
Created many_versions.txt.190
Created many_versions.txt.191
Created many_versions.txt.192
Created many_versions.txt.193
Created many_versions.txt.194
Created many_versions.txt.195
Created many_versions.txt.196
Created many_versions.txt.197
Created many_versions.txt.198
class File
  def File.versioned_file(base, first_suffix='.0', access_mode='w')
    suffix = file = locked = nil
    filename = base
    begin
      suffix = (suffix ? suffix.succ : first_suffix)       
      filename = base + suffix
      unless File.exists? filename
        file = open(filename, access_mode)
        locked = file.flock(File::LOCK_EX | File::LOCK_NB)
        file.close unless locked
      end
    end until locked
    return file
  end
end
File.versioned_file('contested_file')
#<File:contested_file.0> #<File:contested_file.0>
File.versioned_file('contested_file')
#<File:contested_file.1> #<File:contested_file.1>
File.versioned_file('contested_file')
#<File:contested_file.2> #<File:contested_file.2>