看板 Ruby 關於我們 聯絡資訊
※ 引述《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~謝謝.... -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.115.216.102 ※ 編輯: danny8376 來自: 140.115.216.102 (12/17 22:45)
flyingIdea:先推再看!! 12/18 23:38
flyingIdea:原來第二個是指ASCII的範圍!! 12/18 23:42
flyingIdea:謝謝原PO的回文~~ 12/18 23:44