Ruby: how to tell if character is upper/lowercase

I don't think there is something more idiomatic. The only thing you could do -- instead of passing in the string as an argument -- is monkey patch the String class:

class String
  def is_upper?
    self == self.upcase
  end

  def is_lower?
    self == self.downcase
  end
end

"a".is_upper? #=> false
"A".is_upper? #=> true

Using the method in the answer suggested by the commenter above and monkey patching String, you could do this:

class String
  def is_upper?
    !!self.match(/\p{Upper}/)
  end

  def is_lower?
    !!self.match(/\p{Lower}/)
    # or: !self.is_upper?
  end
end

Use a regex pattern: [A-Z] or:

/[[:upper:]]/.match(c)

Tags:

Ruby