Alle Saturday 02 February 2008, Dondi ha scritto:
> I am trying to parse a string and extract all vowels and consonants
> into two separate substrings. However, I can't get my solution to
> work. Any pointers are appreciated. Here is the approach I am using:
>
> 1) Extended the String Class with the following methods:
>
> class String
>   def vowels
>     self.scan(/[aeiou]|(?![aeiou])y(?![aeiou])/i)
>   end
>
>   def consonants
>     self.scan(/![aeiou]|(?=[aeiou])y(?=[aeiou])/i)
>   end
> end
>
> 2) Invoke the methods:
>
> test_paragraph = "Mary had a little lamb"
> @vowel_sub_str = test_paragraph.vowels
> @consonant_sub_str = test_paragraph.consonants
>
> However, the result is just two empty strings. I believe the problem
> is in the regular expression, but I can't figure out just where. Any
> ideas/pointers are appreciated.
>
> Thanks.

A simple (but I fear not too efficient) way is:

class String

 def vowels
   gsub(/[^aeiou]/, '')
 end

 def consonants
   gsub(/[aeiou]/, '')
 end

end

Stefano