[:green_item, :purple_ite-6ren">
gpt4 book ai didi

Ruby 哈希插值

转载 作者:数据小太阳 更新时间:2023-10-29 08:18:47 26 4
gpt4 key购买 nike

我有一个混合类型的散列:

stuff = {:pack_one => ["blue_item", "red_item"], :pack_two => [:green_item, :purple_item, :yellow_item]}

我需要将其转换成如下句子:

"pack_one contains a blue_item and red_item and pack_two contains a green_item, purple_item and yellow_item"

所以我假设我需要使用 Enumerable 并迭代散列并构建句子,但我不确定如何做?

最佳答案

我的解决方案。符合要求比@Eureka 的回答更严格。

strings = stuff.map do |key, values|
"#{key} contains a #{values[0..-2] * ', '} and #{values.last}"
end
puts strings.join ' and '

关于Ruby 哈希插值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13353002/

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