Created
December 1, 2011 11:32
-
-
Save kgrz/1416044 to your computer and use it in GitHub Desktop.
checking for file extension using split vs regex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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