将值存储在数组中并返回函数 ruby

问题描述 投票:0回答:1

item 有哈希数组。前任。 item = [{item1},{item2},{item3}]

def function1
   item.map( other_class.call )
end

# other class will return the array which contains string
class other_class
   def call 
     #method which return arrray of strings
   end
end

当前函数 1 正在返回具有字符串值的数组数组。

function1 output example [["str1"], [], ["str2"], ["str3"], ["str4","str5"]]

我想修改将返回字符串数组的函数 1。

expected output = ["str1" "str2", "str3", "str4", "str5"]

伙计们,知道我该如何修改上述方法。

提前致谢。

arrays ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-4
1个回答
0
投票

输入

item=[["str1"], [], ["str2"], ["str3"], ["str4","str5"]]

代码

p item.flatten

输出

["str1", "str2", "str3", "str4", "str5"]

所以你应该写这样的东西

def function1
   other_class.call.flatten
end
© www.soinside.com 2019 - 2024. All rights reserved.