在ruby中重新打开脚本

时间:2013-10-21 14:05:32

标签: ruby scripting

我想在执行结束时重新打开我的脚本。我尝试加载但它没有用。我想做什么?如果是,我该怎么办?

这是我的脚本的结构:

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

基本上我想通过usr_choice语句后回到用户输入(case)。没有GoTo。

1 个答案:

答案 0 :(得分:1)

所以你想要一个循环?好吧,使用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

示例:

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