【问题标题】:Sample ruby program keeps running with errors, can't figure them out [closed]示例 ruby​​ 程序不断运行并出现错误,无法弄清楚 [关闭]
【发布时间】:2015-12-11 21:33:25
【问题描述】:

好的,我已尽我所能让这段代码运行而不会出现错误,但无济于事。希望你能帮帮我。

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

【问题讨论】:

  • 请不要让我们猜测。编辑您的问题以包含您收到的实际错误消息,包括行号和回溯。

标签: ruby rubygems syntax-error


【解决方案1】:

好的,有一些问题,但不要担心一切都可以 修复了!

让我们从你做得好的地方开始

  • 在大多数情况下使用布尔值做得很好,大多数初学者似乎不会 掌握== 意味着平等,= 意味着完全 不一样的

  • puts 做得很好并正确使用它,还有其他 我稍后会介绍的方法,在你的 案例。

现在让我们介绍一下可以修复的问题

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

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

  • elsif; else; elsif 从不else 之后使用 elsifelsif 是提供一个例外,在你的情况下还有更多 一个选项,所以在 else 之前使用 elsifs,你最好在这里使用 case 语句。
  • 听起来不像是个混蛋,但你的缩进太可怕了。在 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

然后砰!您的程序已启动并正在运行!如果您对任何事情有任何疑问,请不要犹豫,编程可能非常棘手,而且可能非常困难。坚持下去,你会成功的!

【讨论】:

    【解决方案2】:

    我发现很多问题。

    1. 这是无效的:

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

      这也不是:

      while true out_file.puts("test program v1")
      

      不过,我无法告诉您如何解决此问题,因为您根本不清楚您要做什么。

    2. 一个if 块可能只有一个else,而且它毕竟必须是elsifs。

    3. 这里:

      elsif user_input_2 = "TEXTMAKER"
      

      您正在为user_input_2 分配一个新值。我猜你打算使用相等运算符,即==

    4. 您的 def 块没有 end。在我编辑您的代码以使用正确的缩进后,这一点变得很明显。使用合理的缩进和空格可以为自己省去很多麻烦。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-31
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-26
      • 2012-08-23
      • 1970-01-01
      相关资源
      最近更新 更多