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

uwopmtnx  于 2023-03-08  发布在  Ruby
关注(0)|答案(1)|浏览(110)

项目具有哈希数组。例如,项目=[{项目1},{项目2},{项目3}]

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

function1当前返回的数组为array,该数组具有字符串值。
function1 output example [["str1"], [], ["str2"], ["str3"], ["str4","str5"]]
我想修改函数1,它将返回字符串数组。

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

伙计们,有什么想法我可以修改上述方法。
先谢了。

e3bfsja2

e3bfsja21#

输入

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

代码

p item.flatten

输出

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

所以你应该这样写

def function1
   other_class.call.flatten
end

相关问题