0
메서드 내에서 전역 변수를 만들 수 있는지 궁금합니다.Ruby - 메서드 내에서 전역 변수 만들기
그래서 아래 예제에서 메서드 외부에서 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
그러나 메소드 내에서 변수 만 선언하면 작동합니까? gets.chomp로 설정됩니다. 전에 $ 기호를 사용하여이 작업을 시도했지만 작동하지 않는 것 같았습니다. – JoshRicha
작업을보다 명확하게하기 위해 필자의 대답이 업데이트되었지만 여전히 그렇지 않은 경우 언제든지 물어보십시오. 유용하다고 생각되면 답변 옆에있는 체크 표시를 클릭하여 수락하는 것이 좋습니다. – kriskova