To check if a string is a pangram, you can use the following steps in Ruby:
Set
of alphabets from 'a' to 'z'.Set
.Set
is empty. If it is empty, then the input string is a pangram.Here's the Ruby code that implements the above steps:
main.rb657 chars25 lines
In this code, the pangram?
method takes a string as input and returns a boolean value indicating whether it is a pangram or not. We use the gsub
method to remove non-alphabetic characters, downcase
method to convert the string to lowercase, and Set
to store the remaining alphabets. Finally, we iterate over the characters in the cleaned lowercase string and remove them from the set. If the set becomes empty, we break the loop and return true
, otherwise, we return false
.
The output of the above code will be true
, indicating that the input string is indeed a pangram.
gistlibby LogSnag