Ruby-on-rails 如何找到数组类的长度?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3184152/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:51:57  来源:igfitidea点击:

How to find the length of an array class?

ruby-on-railsarraysruby

提问by Milan

$param = k.chomp.split("\t")

How can I find the length of $paramgiven that it is an array? actually when I'm writing

我怎样才能找到$param给定它是一个数组的长度?实际上在我写作的时候

puts $param.class

I got the o/p like Array. Now how do I find the length of this array?

我得到了像 Array 一样的 o/p。现在我如何找到这个数组的长度?

I tried $param.lengthbut it's not working.

我试过了,$param.length但它不起作用。

回答by Salil

Ref Arrayclass

引用数组

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

o/p

length of $param is : 4

回答by bradgonesurfing

Try using .sizelike this:

尝试使用.size这样的:

$param.size