ruby语法有问题

ruby语法有问题,ruby,syntax,Ruby,Syntax,我是ruby新手,不知道为什么我的代码在命令提示符下rake时不能工作。我得到的东西似乎说语法错误,意外的关键字结束,预期输入结束,我不知道如何确切地修复它。任何帮助都将不胜感激,谢谢 def temperature_conversion_functions def ftoc(x) "Converts freezing temperature" ftoc(32) == 0 end "Converts boiling tem

我是ruby新手,不知道为什么我的代码在命令提示符下rake时不能工作。我得到的东西似乎说语法错误,意外的关键字结束,预期输入结束,我不知道如何确切地修复它。任何帮助都将不胜感激,谢谢

def temperature_conversion_functions
    def ftoc(x)
        "Converts freezing temperature"
        ftoc(32) == 0
        end

        "Converts boiling temperature"
        ftoc(212) == 100
        end

        "Converts body temperature"
        ftoc(98.6) == 37
        end

        "converts arbitrary temperature"
        ftoc(68) == 20
        end
    end

    def ctof(x)

        "converts freezing temperature"
        ctof(0) == 32
        end

        "converts boiling temperature"
        ctof(100) == 212
        end

        "converts arbitrary temperature"
        ctof(20) == 68
        end

        "converts body temperature"
        ctof(37) == 98.6
        end
    end

end

在相应的
def
class
if
等关键字下,正确对齐
结束
关键字,问题将被清除。(通过正确的缩进,实际上有几个问题变得相当明显。)

无论如何,考虑转换函数应该仅仅是

然后就可以这样使用了

puts "42f is #{ftoc(42)}c"

你有许多不需要的
end
关键字。代码可以包含在块中,如下所示:

x = 5
if x > 3
  puts "x is greater than 3!"
end
if
end
附件类似,还有其他关键字,如
do
def
class
while
等,都有不同的用法

其他材料:

  • 您似乎在使用字符串注释代码,如
    “转换冻结温度”
    。碰巧ruby将对字符串求值,然后对其不做任何处理。但是,编写注释的正确方法是使用
    #
    符号:

    # This is a comment. The line below is executed code
    puts "Printing out this string"
    
  • 您正在递归
    ftoc
    ctof
    。小心造成无限循环!下面是递归的一个扩展示例:

    def ftoc(x)     # define the `ftoc(x)` method
      ftoc(32) == 9 # let's expand this line
    end
    
    ftoc(32)
    “扩展”为
    ftoc(32)==9
    ,因为这就是
    ftoc(x)
    的定义:

    def ftoc(x)
      ( ftoc(32) == 9 ) == 9 # "expanded" once
    end
    
    再说一遍:

    def ftoc(x)
      ( ( ftoc(32) == 9 ) == 9 ) == 9 # "expanded" twice
    end
    
    并将永远持续下去,永无止境

  • 无需在
    temperature\u conversion\u函数
    方法中定义两个
    ftoc
    ctof
    方法。如果您想将几个相关方法组织在一起,我建议您使用
    模块

    module TemperatureConversion
      def TemperatureConversion.ftoc(f) # This is an example. More idiomatic way is shown below
        return (f - 32) * 5.0/9
      end
    
      def self.ctof(c) # the 'self' in this line means/is-the-same-as 'TemperatureConversion'
        return c * 9.0/5 + 32
      end
    end
    
    # now you can use the module and its methods
    # convert freezing
    puts TemperatureConversion.ftoc(32) # will output 0.0
    

  • “获取似乎是在说的东西”,你的意思是“我看到了一个读起来的错误”?通常,最好逐字逐句地发布错误消息。另外……您在这里想做什么?看起来您正试图编写一个包含这两个函数的名称空间(即模块或类),然后对它们进行文档记录(因为您展示了示例用法),但您编写的不是文档,而是实际的代码(具体来说,您编写的布尔表达式调用您应该定义的函数)。你能解释一下你的想法吗?另外,不清楚“当我在命令提示符中rake它时”是什么意思。rake是一个“类似”的Ruby构建系统,而不是Ruby执行环境。这些是单元测试吗?当然!如果你觉得我的帖子有帮助,你介意接受它作为答案吗?
    def ftoc(x)
      ( ( ftoc(32) == 9 ) == 9 ) == 9 # "expanded" twice
    end
    
    module TemperatureConversion
      def TemperatureConversion.ftoc(f) # This is an example. More idiomatic way is shown below
        return (f - 32) * 5.0/9
      end
    
      def self.ctof(c) # the 'self' in this line means/is-the-same-as 'TemperatureConversion'
        return c * 9.0/5 + 32
      end
    end
    
    # now you can use the module and its methods
    # convert freezing
    puts TemperatureConversion.ftoc(32) # will output 0.0