我是 Ruby 新手,我编写了一个非常简单的应用程序来打印星期几,然后循环删除一天:
def print_days(days)
days.each do |day|
print "The day of the week is: #{day}\n"
days.delete(day)
print "\n*****************************************************\n"
print days
print "\n*****************************************************\n"
end
end
wd = %w[Monday Tuesday Wednesday Thursday Friday Saturday Sunday]
print print_days(wd
这在运行时会给出以下输出。谁能解释为什么当我按顺序删除每个元素并且数组显示它们在那里时,为什么会跳过周二、周四和周六?你可以在你的设置中运行这个简单的代码:
The day of the week is: Monday
*****************************************************
["Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"]
*****************************************************
The day of the week is: Wednesday
*****************************************************
["Tuesday", "Thursday", "Friday", "Saturday", "Sunday"]
*****************************************************
The day of the week is: Friday
*****************************************************
["Tuesday", "Thursday", "Saturday", "Sunday"]
*****************************************************
The day of the week is: Sunday
*****************************************************
["Tuesday", "Thursday", "Saturday"]
*****************************************************
["Tuesday", "Thursday", "Saturday"]