2010-07-06 78 views
27
$param = k.chomp.split("\t") 

如何找到$param的長度,因爲它是一個數組? 其實當我在寫如何查找數組類的長度?

puts $param.class 

我喜歡陣列的O/P。 現在我該如何找到這個數組的長度?

我試過$param.length但它不起作用。

+0

你確定你做了'$ param.length'不是'$ param.lenght'?你收到什麼錯誤信息? – 2010-07-06 07:09:43

回答

22

嘗試使用.size這樣的:

$param.size 
+3

大小隻是長度的別名。 – Aurril 2010-07-06 07:12:38

30

Array價類

k = "This \t is \t just \t testing" 
$param=k.chomp.split("\t") 
array_length= $param.length #or $param.size 
puts "length of $param is : #{array_length}" 

O/P

length of $param is : 4 
+0

謝謝salil $ param.size正在工作,但$ param.length不起作用 – Milan 2010-07-06 08:08:40

+1

什麼是您的ruby版本? – Salil 2010-07-06 08:14:52

2

作品與:

Array.length 

Array.size 
+0

已經在其他答案中更好地回答了答案 – Isaac 2017-11-14 23:17:34