简体   繁体   English

重新打开Ruby脚本

[英]Re-open a script in ruby

I would like to re-open my script at the end of its execution. 我想在执行结束后重新打开我的脚本。 I tried with load but it didn't work. 我尝试加载,但是没有用。 Is what I want to do possible? 我想做的事可能吗? If yes, what should I try? 如果是,我该怎么办?

This is the structure of my script: 这是我的脚本的结构:

usr_choice = gets.chomp.to_i 
case usr_choice 
  when 1 
   # Do something
  when 2 
   # Do something else
  else 
   puts "Choice not recognised"
end 

Basically I would like for example to go back to the user input ( usr_choice ) after going through the case statement. 基本上,我想例如通过case语句后返回到用户输入( usr_choice )。 Without a GoTo. 没有GoTo。

So you want a loop? 所以你想循环吗? Well, use loop : 好吧,使用loop

loop do
  usr_choice = gets.chomp.to_i

  case usr_choice
  when 1
    puts 'one'
  when 2 
    puts 'two'
  else
    break # exit the loop (not the case construct as in other languages)
  end
end

Example: 例:

$ ruby filename.rb
1
one
2
two
1
one
kthxbye
$ 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM