Skip to content

Instantly share code, notes, and snippets.

@djberg96
Created January 17, 2012 19:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djberg96/1628448 to your computer and use it in GitHub Desktop.
Save djberg96/1628448 to your computer and use it in GitHub Desktop.
Custom mktemp for Ruby
class Foo
def mktemp(template)
regex = /^([^X].*?)(X)(XXXXX)/
unless m = regex.match(template)
raise "Invalid template: #{template}"
end
chars = ('a'..'z').to_a
result = m.captures[0] + chars[rand(25)]
5.times{ result << rand(9).to_s }
result
end
end
p Foo.new.mktemp("rb_file_temp_XXXXXX")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment