Forum: Ruby A stupid question?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D360f53ad977731c8a81610cf94e1384?d=identicon&s=25 Al B. (alcheddah)
on 2012-12-21 01:41
I'm trying to find a way to count the consonants in a sentence. how
would i go about that? i tried to use length - [aeiou]. but never got
the result i was looking for
5989a003d261c0abe6d08570ff6cc3d0?d=identicon&s=25 7stud -. (7stud)
on 2012-12-21 02:03
str = 'Hello, world!?'
puts str.count("a-zA-z", "^aeiou")

--output:--
7
5989a003d261c0abe6d08570ff6cc3d0?d=identicon&s=25 7stud -. (7stud)
on 2012-12-21 02:24
7stud -- wrote in post #1089771:
> str = 'Hello, world!?'
> puts str.count("a-zA-z", "^aeiou")
>

Whoops.  That should be ... count("a-zA-Z", "^aeiou"). You can read that
statement as: count() all the characters which are in the ranges
"a-zA-Z" AND which are not "aeiou".
686ff759a5882c193c9bd0af32a2f0d0?d=identicon&s=25 Brian C. (candlerb)
on 2012-12-21 16:56
Al Baker wrote in post #1089770:
> I'm trying to find a way to count the consonants in a sentence. how
> would i go about that? i tried to use length - [aeiou]. but never got
> the result i was looking for

>> str = 'Hello, world!?'
>> str.scan(/[a-df-hj-np-tv-z]/i).size
=> 7
>> str.gsub(/[^a-df-hj-np-tv-z]/i,'').size
=> 7
tamouse mailing lists (Guest)
on 2012-12-22 14:17
(Received via mailing list)
n Fri, Dec 21, 2012 at 9:56 AM, Brian Candler <lists@ruby-forum.com>
wrote:
> Al Baker wrote in post #1089770:
>> I'm trying to find a way to count the consonants in a sentence. how
>> would i go about that? i tried to use length - [aeiou]. but never got
>> the result i was looking for
>
>>> str = 'Hello, world!?'
>>> str.scan(/[a-df-hj-np-tv-z]/i).size
> => 7
>>> str.gsub(/[^a-df-hj-np-tv-z]/i,'').size
> => 7

(a is a vowel...)

I'd suggest this:

 > s = 'Helloooo my wonderfuuuuul peeeopulllaaaa. IIII am come!!'
 => "Helloooo my wonderfuuuuul peeeopulllaaaa. IIII am come!!"
 > s.scan(/[^aeiou]+/i).join('')
 => "Hll my wndrfl pplll.  m cm!!"
 > s.scan(/[^aeiou]+/i).join('').size
 => 28
tamouse mailing lists (Guest)
on 2012-12-22 14:31
(Received via mailing list)
On Sat, Dec 22, 2012 at 7:17 AM, tamouse mailing lists
<tamouse.lists@gmail.com> wrote:
>> => 7
>  => 28
... and that counts non-alphas.

s.scan(/[[:alpha:]]+/i).join.scan(/[^aeiou]+/i).join.size
This topic is locked and can not be replied to.