Forum: Ruby A stupid question?

Be409c357065b75bd5dda1424e6976f8?d=identicon&s=25 Al Baker (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
54404bcac0f45bf1c8e8b827cd9bb709?d=identicon&s=25 7stud -- (7stud)
on 2012-12-21 02:03
str = 'Hello, world!?'
puts str.count("a-zA-z", "^aeiou")

--output:--
7
54404bcac0f45bf1c8e8b827cd9bb709?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".
753dcb78b3a3651127665da4bed3c782?d=identicon&s=25 Brian Candler (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
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 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
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 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
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.