The regex version of the def isogram?(w) = p !w.match(/(.)(?:\1|-|\d)/i)
in Ruby would be the following:
main.rb54 chars4 lines
Explanation:
word =~ /(.)(?:\1|-|\d)/i
is the regular expression pattern that checks for the presence of a repeated character (\1
represents the same character as group 1), a hyphen (-
), or a digit (\d
).!
operator is used to negate the result of the match, so if any of these patterns are found, the method returns false
, indicating that it is not an isogram. Otherwise, it returns true
, indicating that it is an isogram.Keep in mind that Ruby supports multiple ways of defining methods, and both examples are functionally equivalent.
gistlibby LogSnag