To collect only the letters from the given string without using the .downcase
method in Ruby, you can use the gsub
method along with a regular expression.
Here's an example of how you can do it:
main.rb122 chars6 lines
This will output:
main.rb32 chars2 lines
The gsub
method is used to replace all non-alphabetic characters with an empty string. The regular expression /[^a-zA-Z]/
matches any character that is not a letter (both lowercase and uppercase). By replacing these non-alphabetic characters with an empty string, you get a string consisting only of letters.
Note that in the given string, the backslashes are escaped by another backslash to ensure they are treated as literal characters.
Keep in mind that this solution is case-sensitive, meaning that uppercase and lowercase letters will not be converted to a specific case.
Hope this helps!
gistlibby LogSnag