If the directory already exists, mkdir raises exception. To prevent this:
Dir.mkdir(dir) unless File.exists?(dir)