【发布时间】:2021-08-14 14:21:10
【问题描述】:
我最近解决了这个问题,但觉得有一种更简单的方法可以解决。我研究了注入、步进和映射,但不知道如何在这段代码中实现它们。我想使用比现在更少的代码行。我是 ruby 的新手,所以如果答案很简单,我很乐意将它添加到我的工具包中。提前谢谢你。
目标:接受一个句子字符串作为arg,并返回包含大写和小写交替单词的句子
def alternating_case(str)
newstr = []
words = str.split
words.each.with_index do |word, i|
if i.even?
newstr << word.upcase
else
newstr << word.downcase
end
end
newstr.join(" ")
end
【问题讨论】:
标签: ruby-on-rails ruby rspec