【发布时间】:2017-07-28 22:21:50
【问题描述】:
如何访问在函数外部定义的数组?我试过添加$coins.length,因为我在某处读过全局变量——它没有用。我在外面定义数组的原因是因为下面还有其他函数会将新项目推送到同一个数组中。
coins = []
def start
puts "It's a sunny day. It's warm and you feel great."
puts "You've picked up your son from creche. He's in a good mood and you've got home no problem."
puts "This is when troubles start, and you know it."
puts "You go out of the elevator and before you reach the door... 'give me the keeys'!"
puts "Do you give him the keys? (Y/N)"
print "> "
while keys = gets.chomp.downcase
case keys
when "y"
puts "you are in, good job. And you get a coin"
coins.push("1")
puts "you now have #{coins.length} coins"
room
when "n"
cry("I wanted to dooooooo iiiiiiiit!")
else
again
end
end
end
【问题讨论】:
标签: ruby