2014-10-08 72 views
-1

我得到了这些错误 -因式分解红宝石中的多项式练习,意外的语法?

(eval):30: (eval):30: compile error (SyntaxError) 
(eval):8: syntax error, unexpected kDO 

- 当我跑在控制台这个Ruby代码 -

class Factor 
    puts "This program will factor a polynomial in the form of \"Ax^2 + Bx + C.\"" 

    aPos = PosOrNeg?("A") 

    # test 
    puts aPos 

    def PosOrNeg?(string = "blank") do 
     puts "Tell us if \"#{string}\" is positive or negative. Type \"pos\" or \"neg\"." 
     posOrNegStr = gets.chomp 
     case posOrNegStr 
     when "pos" 
      pos = true 
     when aPosOrNegStr = false 
      pos = false 
     else 
      while posOrNegStr != "pos" && posOrNegStr != "neg" do 
       puts "Invalid input. Please type \"pos\" or \"neg\"." 
       posOrNegStr = gets.chomp 
       case posOrNegStr 
       when "pos" 
        pos = true 
       when aPosOrNegStr = false 
        pos = false 
       else 
       end 
      end 
     end 
    end 
end 

的思考?

+0

从行删除'do':def PosOrNeg?(string =“blank”)'然后尝试 – Surya 2014-10-08 18:02:54

+1

你的问题是什么? – sawa 2014-10-08 18:12:38

+1

值得注意的是,Ruby方法和变量应该遵循'lower_case_with_underscores'约定。使用大写字母是不寻常的,正如您在这里看到的,会混淆语法突出显示。 – tadman 2014-10-08 18:18:16

回答

3

看行:

# test 
puts aPos 

def PosOrNeg?(string = "blank") do # <- here!! 

下面是在Ruby中定义一个方法的理想语法:

def method_name 
    # method body 
end 

因此,改变def PosOrNeg?(string = "blank") dodef PosOrNeg?(string = "blank")

注意:在Ruby中,用于定义方法或对象的编码风格是snake_cased,它在Ruby社区中非常有名。如果您将您的方法命名为:pos_or_neg?(string = 'blank'),则会更好。

+0

感谢你们所有人!我对Ruby非常陌生(来自Java,正如你可能认为我的骆驼外壳一样)。 – 2014-10-08 18:48:04

+0

@DanielBaskin:欢迎来到Ruby社区。如果它对你有帮助,请接受答案(这在SO社区非常有名,并表示感谢)。另外,我猜CamelCase在JavaScript社区中也是非常有名的编码风格。 :) – Surya 2014-10-08 18:52:31