count 方法
用來計算陣列之中的元素或是某指定元素的數量。
nums = [0,2,4,0,3,0]
nums.count <span class="com"># 單純計算陣列元素數量。得6</span>
nums.count(0) <span class="com"># 計算陣列中0的數量。得3</span>
nums.count{|x| x - 2 > 0} <span class="com"># 計算陣列中某元素「x」,且此元素符合「x - 2 > 0」。得2</span>
include 方法
掃描變數是否包含某項元素。
super.include?"s" <span class="com"># 回傳true。</span>
gsub 方法
替換變數中的某元素
super.gsub!(/s/, "th") <span class="com"># 將「//」中的元素變換成「""」中的元素。</span>
split 方法
將字串拆解成陣列。
"super man".split <span class="com"># ["super", "man"]</span>
" super man".split("") <span class="com"># [" ","s","u","p","e","r"," ","m","a","n"]</span>
" super man".split(//) <span class="com"># [" ","s","u","p","e","r"," ","m","a","n"]</span>
" super man".split(" ") <span class="com"># ["super", "man"]</span>
" super man".split(/ /) <span class="com"># ["","super", "man"]</span>
"super".split(//, 3) <span class="com"># ["s", "u", "per"]</span>
"super man".split(//, 3) <span class="com"># ["s", "u", "per man"]</span>
"super super".split("upr") <span class="com"># ["s", "r s", "r"]</span>
"1,2,,3,4,,".split(',') <span class="com"># ["1", "2", "", "3", "4"]</span>
"1,2,,3,4,,".split(',', 4) <span class="com"># ["1", "2", "", "3,4,,"]</span>
"1,2,,3,4,,".split(',', -4) <span class="com"># ["1", "2", "", "3", "4", "", ""]</span>
"".split(',', -1) <span class="com"># []