當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Ruby Array clear()用法及代碼示例


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:[]



相關用法


注:本文由純淨天空篩選整理自mayank5326大神的英文原創作品 Ruby | Array clear() operation。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。