Skip to content

Instantly share code, notes, and snippets.

@jessereynolds
Created March 14, 2014 05:42
Show Gist options
  • Select an option

  • Save jessereynolds/9542669 to your computer and use it in GitHub Desktop.

Select an option

Save jessereynolds/9542669 to your computer and use it in GitHub Desktop.
ruby refactoring multiple unlesses
# before:
if command == 'move'
unless options.destination
puts "Error - 'move' requires a --destination"
puts "\n#{optparse}"
end
unless File.exist?(options.destination)
puts "Error - destination does not exist"
puts "\n#{optparse}"
end
unless File.directory?(options.destination)
puts "Error - destination is not a directory"
puts "\n#{optparse}"
end
unless File.writeable?(options.destination)
puts "Error - destination directory is not writeable"
puts "\n#{optparse}"
end
end
# after
if command == 'move'
error = case
when !options.destination
"Error - 'move' requires a --destination"
when !File.exist?(options.destination)
"Error - destination does not exist"
when !File.directory?(options.destination)
"Error - destination is not a directory"
when !File.writeable?(options.destination)
"Error - destination directory is not writeable"
end
if error
puts "#{error}\n#{optparse}"
exit 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment