示例ruby程序一直运行时出错,无法解决

时间:2015-12-11 21:33:26

标签: ruby rubygems syntax-error

好吧,我已尽力让这段代码在没有吐出错误的情况下运行,但无济于事。希望你能帮助我。

require 'launchy'

#def linebreak(breakline)

def program()
    puts "Welcome to test program v1. Would you like to continue? ENTER y for Yes or n for no"
    user_input_1 = gets.chomp

    if user_input_1 == "y"
        puts "How would you like to proceed CRASH | TEXTMAKER | UNDECIDED // CASE SENSITIVE"
        user_input_2 = gets.chomp

        if user_input_2 == "CRASH"
            while true Launchy.open("http://google.com")
        elsif user_input_2 = "TEXTMAKER"
            while true out_file.puts("test program v1")
        else
            puts "You have not entered a method."
        elsif user_input_1 == "n"
            abort
        else
            puts "That is not a valid command. Please run the script again."
        end
    end

2 个答案:

答案 0 :(得分:0)

好吧,有一些问题,但不要担心一切都可以 固定!

让我们从你做得很好

开始
  • 在大多数情况下使用你的布尔值很好,大多数初学者似乎都没有 掌握==意味着平等,=完全意味着什么 不同

  • puts干得好,使用得当,还有其他 我将在稍后介绍的方法,在你的方面会更好看 情况下。

现在让我们来讨论可修复的内容

  • 如上所述,在大多数情况下,您正确使用了布尔值 但是你似乎错过了一个。 elsif user_input_2 = "TEXTMAKER"您需要==来证明它是平等的。

  • 您的while循环似乎没有任何意义,因为您并未真正将true设置为任何内容。在Ruby中它是高度的 建议永远不要使用while循环,当然有时候 你必须这样做,但只有在你尝试了其他一切之后。

  • elsif; else; elsif 从不elsif之后使用elseelsif是提供例外情况,在您的情况下会有更多例外 一个选项,所以在其他地方之前使用elsif,你最好在这里使用一个案例陈述。
  • 听起来不像屁股,但你的缩进很可怕。你会发现很多问题都有很好的缩进,在Ruby中是两个 空格,这里是风格指南: https://github.com/bbatsov/ruby-style-guide
  • 永远不要让您的用户选项区分大小写,如果其他人打算使用您的程序,只要假设他们是世界上最愚蠢的人并让他们变得非常容易。 user_input_2.gets.chomp.upcase这将设置它,以便无论他们如何输入文本,它始终都是大写。
  • 您错过了end一旦您使用了适当的缩进,这一点就会变得清晰

好吧,让我们改写这件事:

require 'launchy'

def program #<= you don't need the parentheses
  print "Welcome to the test program version 1, to continue type 'c' to exit type 'e': "
  user_input = gets.chomp.upcase
  if user_input == "C"
    program_choices
  elsif user_input == "E"
    puts "Exiting.."
    exit
    #if you really want to use abort
    #abort("Exiting..") 
    #you won't need to use exit if you use abort
  else 
    puts "Invalid input, try again"
    program #<= this is a loop known as recursion, don't use it to much
  end
end

def program_choices
  print "Pick an option, the choices include CRASH | TEXTMAKER | UNDECIDED: "
  user_input = gets.chomp.upcase
  case user_input #<= this is how you start a case statement
  when "CRASH"
    Launchy.open("http://google.com")
  when "TEXTMAKER"
    write_test_data
  when "UNDECIDED"
    puts "You really need to make up your mind.." #<= have some fun with it, that's the point of programming.
    program_choices
  else
    puts "Invalid input, try again"
    program_choices
  end
end

def write_test_data
  x = "Test file version 1"
  if File.exist?("path/to/test/file.txt")
    File.open("path/to/test/file.txt", "a"){ |s| s.write(x) }
  else 
    new_test_file = File.new("path/to/test/file.txt")
    new_test_file.puts(x)
  end
end

和bam!您的程序已启动并运行!如果您对任何不要犹豫的问题有任何疑问,编程可能会非常棘手,而且可能非常困难。坚持下去,你就到了那里!

答案 1 :(得分:-1)

我看到很多问题。

  1. 这不是有效的:

    while true Launchy.open("http://google.com")
    

    这两个都不是:

    while true out_file.puts("test program v1")
    
    但是,我无法告诉你如何解决这个问题,因为它并不清楚你正在尝试做什么。

  2. if区块可能只有一个else,并且必须在所有elsif之后。

  3. 下面:

    elsif user_input_2 = "TEXTMAKER"
    

    您正在为user_input_2分配新值。我猜你是想使用等于运算符,==

  4. 您的def区块没有end。在我编辑代码以使用适当的缩进后,这变得很明显。通过使用合理的缩进和空格,你可以省去很多麻烦。