Array#clear():clear()是一個Array類方法,可從其中刪除所有數組元素。
用法: Array.clear() 參數: Arrays to clear off 返回: array with all elements cleared
代碼#1:clear()方法示例
# Ruby code for clear() method
# declaring array
a = [1, 2, 3, 4]
# declaring array
b = [111.11, 2.5, 4.3, 2.224]
# clearing the arrays
puts "clear a:#{a.clear()}\n\n"
puts "clear b:#{b.clear()}\n\n"
輸出:
clear a:[] clear b:[]
代碼2:clear()方法示例
# Ruby code for clear() method
# declaring array
a = ["abc", "xyz", "dog"]
# declaring array
b = ["cow", "cat", "dog"]
# declaring array
c = ["cat", "1", "dog"]
# clearing the array
puts "clear a:#{a.clear()}\n\n"
puts "clear b:#{b.clear()}\n\n"
puts "clear c:#{c.clear()}\n\n"
輸出:
clear a:[] clear b:[] clear c:[]
相關用法
- Ruby Array at()用法及代碼示例
- Ruby Array map!()用法及代碼示例
- Ruby Array dig()用法及代碼示例
- Ruby Array any?()用法及代碼示例
- Ruby Array cycle()用法及代碼示例
- Ruby Array compact()用法及代碼示例
- Ruby Array concat()用法及代碼示例
- Ruby Array bsearch()用法及代碼示例
- Ruby Array count()用法及代碼示例
- Ruby Array bsearch_index()用法及代碼示例
- Ruby Array compact!()用法及代碼示例
- Ruby Array intersection用法及代碼示例
- Ruby Array delete_at()用法及代碼示例
- Ruby Array combination()用法及代碼示例
- Ruby Array size()用法及代碼示例
注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array clear() operation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。