Ruby determining whether a letter is uppercase or not
ruby
Solution
You can use POSIX character classes:
- `/[[:lower:]]/` - Lowercase alphabetical character
- `/[[:upper:]]/` - Uppercase alphabetical
Example:
def which_case(letter)
case letter
when /[[:upper:]]/
:uppercase
when /[[:lower:]]/
:lowercase
else
:other
end
end
which_case('a') #=> :lowercase
which_case('ä') #=> :lowercase
which_case('A') #=> :uppercase
which_case('Ä') #=> :uppercase
which_case('1') #=> :other
Or with a simple `if` statement:
puts 'lowercase' if /[[:lower:]]/ =~ 'a'
#=> lowercase
Problem
The question is very simple and probably have thousand of answers, but i am looking for some magical ruby function. Problem: `To determine whether a letter is upcase or not i.e belongs to A-Z.` Possible Solution: ``` array = ["A","B", ....., "Z"] letter = "A" is_upcase = array.include? letter ``` Please note that "1" is not an uppercase letter. Is there any magical ruby function which solve the problem with less code?