Last active
December 31, 2015 23:49
-
-
Save Karunamon/8062346 to your computer and use it in GitHub Desktop.
A monkey patch to return enumerable ranges from arbitrary strings
This file contains 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
class String | |
def to_r | |
a = self.partition(/\(.+?\)/) | |
fail "No range in string" if a[0].empty? | |
fail "Invalid range #{a}" if a.select { |v| v.match(/[A-Za-z]/) } | |
eval a[1] | |
end | |
def to_r_leading_zero | |
a = self.partition(/\(.+?\)/) | |
fail "No range in string" if a[0].empty? | |
fail "Invalid range #{a}" if a.select { |v| v.match(/[A-Za-z]/) } | |
r = eval a[1] | |
if r.first.to_s.size == 1 | |
eval "\'#{"0" + r.first.to_s}\'..\'#{r.last.to_s}\'" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment