gpt4 book ai didi

ruby - 为什么我不能获取数组中每个字符串的数组长度?

转载 作者:太空宇宙 更新时间:2023-11-03 17:18:04 28 4
gpt4 key购买 nike

我想弄清楚如何获取数组值的长度(索引?)出于某种原因,它返回 1,0,0,我不知道为什么。谁能解释一下我做错了什么?仅供引用,这是来自 RubyMonk。我试图在没有得到答案的情况下解决它..只需要一点点提升来弄清楚我做错了什么

def length_finder(input_array)
output= []
input_array.length.times do |x|
output << input_array.length[x]
end
return output
end

最佳答案

不应该是:

output << input_array[x].length

此处您使用的是 Bit reference method .

更多 Ruby 风格:

def length_finder(input_array)
input_array.map(&:length)
end

关于ruby - 为什么我不能获取数组中每个字符串的数组长度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8001839/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com