【发布时间】:2015-08-30 03:34:07
【问题描述】:
在不更改任何其他代码的情况下- 我可以在我的 if else 语句中添加什么以使数组 0、2、4 显示为全大写字母?我不知道如何在 if 和 else 语句中分配它。
test = []
puts "Please type 5 different words when you're ready-"
5.times do
test << gets.chomp
end
test.sort.each do |user|
if #what could I put in this statement for all capital letters on [0,2,4]?
user.upcase
else #.downcase isn't needed, if its simpler to remove it that is fine
user.downcase
end
end
【问题讨论】:
-
是否有不能修改其他代码的原因?因为显而易见的选项是
with_index。 -
有一种方法可以在没有 with_index 的情况下执行此操作,但我无法弄清楚。这是我必须构建它的方式,我并不清楚该方法
-
@whatabout11 没有其他办法(除了在循环中更新您自己的运行变量)。在stackoverflow.com/questions/533837/…
each_with_index上使用each_with_index有一些答案可以澄清和扩展 -
我认为你的权利@thomasfuchs 已经盯着这个看了一段时间
标签: ruby