alilland alilland - 3 months ago 12
Ruby Question

how to return true or false when a string contains any letters?

I am familiar with how to check if a string contains a substring, and also familiar with how to check if a single letter is a number or a letter, but how would I go about checking a string for any letters?

def letters?(string)
# what do i do here?

# string could be anything from '111' to '1A2' to 'AB2589A5' etc...

string = '1A2C35'
if letters?(string) == true
# do something if string has letters
# do something else if it doesnt


I think, you can try something like it:

def letters?(string)
   string.chars.any? { |char| ('a'..'z').include? char.downcase }

If you don't wanna use regexp. This method return true if there are any letters in the string:

> letters? 'asd'
 => true 
> letters? 'asd123'
 => true 
> letters? '123'
 => false