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.
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.
Not sure if OP excluded non letters from the input
class String
def vowels
gsub(/[^aeiou]/, '')
end
def consonants
gsub(/[aeiou]/, '')
end
end
Stefano
This shall do it in one run, quite complicated code, interested if
something more elegant can be found.
class String
def v_c
each_char.inject(["",""]){ |r, c|
case c
when /[aeiouy]/i
[r.first << c, r.last ]
when /[^a-z]/i
r
else
[r.first, r.last << c ]
end
}
end
end
On Feb 2, 2008 12:38 PM, Robert Dober <robert.dober@gmail.com> wrote:
>
> A simple (but I fear not too efficient) way is:
Not sure if OP excluded non letters from the input
>
> class String
>
> def vowels
> gsub(/[^aeiou]/, '')
> end
>
> def consonants
> gsub(/[aeiou]/, '')
> end
>
> end
>
> Stefano
>
>
>
This shall do it in one run, quite complicated code, interested if
something more elegant can be found.
class String
def v_c
each_char.inject(["",""]){ |r, c|
case c
when /[aeiouy]/i
[r.first << c, r.last ]
when /[^a-z]/i
r
else
[r.first, r.last << c ]
end
}
end
end
NoMethodError: undefined method `each_char' for "coreyhaines":String
Why not use split(//) instead of each_char?
···
On Feb 2, 2008 5:04 PM, Robert Dober <robert.dober@gmail.com> wrote:
On Feb 2, 2008 7:27 PM, Corey Haines <coreyhaines@gmail.com> wrote:
> Elegant? I don't know, but I kind of like it.
>
> "corey".split(//).partition { |x| "aeiouAEIOU" }
>
> returns
>
> => [["o", "e"], ["c", "r", "y"]]
>
> irb(main):006:0> "corey haines".split(//).partition { |x|
"aeiouAEIOU" }
> => [["o", "e", "a", "i", "e"], ["c", "r", "y", " ", "h", "n", "s"]]
>
> you might want to skip spaces
>
>
> -Corey
Excellent Corey, allow me to make it fit *any* string, I still have
not seen any statement that we are treating letters only