Re: [问题] COUNT的有些用法看不太懂

楼主: danny8376 (钓到一只猴子@_@)   2013-12-17 22:43:20
※ 引述《flyingIdea (飞翔的想法)》之铭言:
: http://ruby-doc.org/core-2.0.0/String.html
: 的count([other_str]+) → fixnum
: 这个章节
: 我看不太懂他\\的某些意思
: 1.
: "hello^world".count "\\^aeiou" #=>4
: 应该是指 e*1 ^*1 o*2吧?
: 但是我改成
: "hello^world".count "^aeiou" # => 8
: 为什么会变成8@@?
说明好难看懂啊~~~~
http://ruby-doc.org/core-2.0.0/String.html#method-i-count
Each other_str parameter defines a set of characters to count.
The intersection of these sets defines the characters to count in str.
Any other_str that starts with a caret ^ is negated.
The sequence c1-c2 means all characters between c1 and c2.
The backslash character can be used to escape ^ or - and
is otherwise ignored unless it appears at the end of a sequence or
the end of a other_str.
简单来说^开头意思就是aeiou以外的字
自己算一下 有8个
: 2.
: "hello world\\r\\n".count "X-\\w" #=> 3
: 这里也看不太懂"X-\\w"是什么意思
上面第二段红字
\\(因为本身是跳脱字符 所以打\要打成\\)当在^或-前面是用来跳脱用的
也就是上面提到的^(排除)或-(范围)
会变成单纯找^或-这些字 而不是带有上述功能
除此之外都排除掉(无视这字符)
不过如果\\是出现在-的后面 或者是在整个字串最后 则会计入而不无视
所以翻译~~~
X-\\w
X-\\ 外加 w
X-\\ => ASCII 范围 => XYZ[\
(这边出现在-后面 所以会计入)
所以这段找到的文字是
hello world\\r\\n
: 3.
: "hello world".count "lo", "o" #=> 2
: 是因为hello world 所以是二次吗?
: 感觉后面变二个参数后 就由字符比对变成字串比对了
: 是吗?
其实结果就是
"hello world".count "o"
遇到多个参数会取交集
: 4.
: 另外再外插一件和count无关的问题
: books["Gravity's Rainbow"] = :splendid
Hash pair
key: "Gravity's Rainbow" (String)
value: :splendid (Symbol)
: 这个是什么意思?
: 请版上的各位先进帮我解答QQ~谢谢....
作者: flyingIdea (飞翔的想法)   2013-02-18 23:38:00
先推再看!!原来第二个是指ASCII的范围!!谢谢原PO的回文~~

Links booklink

Contact Us: admin [ a t ] ucptt.com