Skip to content

Instantly share code, notes, and snippets.

@Karunamon
Last active December 31, 2015 23:49
Show Gist options
  • Save Karunamon/8062346 to your computer and use it in GitHub Desktop.
Save Karunamon/8062346 to your computer and use it in GitHub Desktop.
A monkey patch to return enumerable ranges from arbitrary strings
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