To extract only the words from the given string in Ruby, you can use regular expressions and the scan
method. Here's how you can do it:
main.rb119 chars4 lines
The scan
method accepts a regular expression as an argument and returns an array of all matching substrings. In this case, we are using the \w+
regular expression which matches one or more word characters (letters, digits, or underscores).
Running the above code will output:
main.rb38 chars2 lines
This extracts only the words from the given string and stores them in the words
array.
gistlibby LogSnag