Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I'm ashamed to ask this, because it seems like it ought to be obvious, but how does one tell whether a given character in a string is upper or lowercase in Ruby? I see no obvious canned solution in the String class.

I've resorted to the following, which does not consider non-ASCII codes:

def is_lower?(c)
  c >= 'a' && c <= 'z'
end

def is_upper?(c)
  ! is_lower(c)
end

Something else I've considered is:

def is_lower?(c)
    c != c.upcase
end

Is there something more idiomatic for this?

question from:https://stackoverflow.com/questions/12713251/ruby-how-to-tell-if-character-is-upper-lowercase

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
356 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...