【发布时间】:2014-10-31 07:19:42
【问题描述】:
我想知道是否可以从方法中创建全局变量。
所以在下面的示例中,我想从方法外部重新使用 s_name 变量。我该怎么做?
# start method
def start
# Start the story
puts "Hello and welcome to the superhero space station, my name is Zakhtar and I am a beautiful mermaid."
puts "Please state your superhero name"
# Gets superhero name
print "> "
# The dollar sign should give the vaiable global scope. Check!
s_name = gets.chomp
# Says hello to the superhero
puts "Pleased to meet you #{s_name}, we are in urgent need of your help!"
# Line break
puts "\n"
puts "Follow me and I will show you the problem..."
death
# end start method
end
【问题讨论】: