Is this a bug of String#count?

R

Ruohao Li

$ ruby -v
ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-darwin9]
$ irb --simple-prompt=> 0

Why the above result is 0 not 1? After all there are 2 characters
in the string s. Is this a bug of String#count?

Thanks in advance.
Ruohao
 
R

Roger Pack

s = "a\u4e00"
=> 0

Why the above result is 0 not 1? After all there are 2 characters
in the string s. Is this a bug of String#count?

count returns the sum of occurrences of characters. I don't see any ^a
in the original string...
 
R

Ruohao Li

Roger said:
count returns the sum of occurrences of characters. I don't see any ^a
in the original string...

But according to the documentation, "Any _other_str_ that starts with a
caret (^)
is negated", thus the following behavior:
$ irb --simple-prompt=> 2
There are two characters in s that is *not* "a".
 
R

Roger Pack

Ruohao said:
But according to the documentation, "Any _other_str_ that starts with a
caret (^)
is negated", thus the following behavior:

Probably because non-ASCII aren't counted as /\w/ matching anymore. You
might want to ping core to see if it is expected or not.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top