Ruby: Extracting only uppercase letters from a string
Recently I was working on a project that called for me to extract the uppercase letters from a string and then in one or another, manipulate these letters.
So, to easily do this, I simply extending the String Ruby object by doing the following
class String
def extract_upper_case_letters
self.scan /\p{Upper}/
end
end
# Implementation
str = "Hello World"
letters = str.extract_upper_case_letters
# ["H", "W"]
Written by dennis monsewicz
Related protips
1 Response
how to do the same for lower case letters and special characters?
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Ruby
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#