ruby中的因式分解多项式练习,意外语法



我得到了这些错误--

(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

想法?

查看行:

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

以下是在Ruby中定义方法的理想语法:

def method_name
  # method body
end

因此,将def PosOrNeg?(string = "blank") do更改为def PosOrNeg?(string = "blank")

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

最新更新