how do i collect the letters only from "\"five quacking zephyrs jolt my wax bed.\"" without using .downcase method? in ruby

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.rb
string = "\\\"Five quacking zephyrs jolt my wax bed.\\\""

letters_only = string.gsub(/[^a-zA-Z]/, '')

puts letters_only
122 chars
6 lines

This will output:

main.rb
Fivequackingzephyrsjoltmywaxbed
32 chars
2 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!

related categories

gistlibby LogSnag