To use the Ruby isogram checker p !/(.).*\1|\d/i
, you can pass the word that needs to be checked as a string argument to the match
method of the regular expression. Here's an example of how to do it:
main.rb60 chars4 lines
In this example, the word
variable holds the word that you want to check for being an isogram. The regular expression /
(.)*\1|\d/iis used to check for any repeated characters or digits. The
matchmethod checks if the regular expression matches any part of the word. If a match is found, the method returns a
MatchDataobject which evaluates to a truthy value. If no match is found, it returns
nilwhich evaluates to a falsy value. By negating the result with
!, we get
trueif the word is an isogram and
false` otherwise.
So in the example above, if the word "isogram" is passed to the word
variable, the output will be true
.
gistlibby LogSnag