Skip to content

Instantly share code, notes, and snippets.

@kgrz
Created December 1, 2011 11:32
Show Gist options
  • Select an option

  • Save kgrz/1416044 to your computer and use it in GitHub Desktop.

Select an option

Save kgrz/1416044 to your computer and use it in GitHub Desktop.
checking for file extension using split vs regex
# The is_valid?(filename) checks if the filename has either a .txt.gz or a .txt extension
def is_valid?(filename)
arr = filename.split(/\.|\/|\\/)
return true if arr.last == "txt"
if arr.last = "gz"
return true if arr[-2, 1] == "txt"
end
return false
end
# becomes
def is_valid?(filename)
filename =~ /[\.txt\.gz|\.txt]$/ ? true : false
end
# Regex rocks!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment