Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby从方法内部但在循环外部返回_Ruby_Loops_Methods_Return - Fatal编程技术网

Ruby从方法内部但在循环外部返回

Ruby从方法内部但在循环外部返回,ruby,loops,methods,return,Ruby,Loops,Methods,Return,尝试运行此代码 运行method1时,将返回两次散列,这意味着将按照“puts method1().inspect”命令的预期返回和打印散列 当method2运行时,第二次退出循环时,通过键入“no”或“n”,将打印一组看似随机的数字,而不是一个可爱的散列。这是为什么 def method1 loop do print "Item name: " item_name = gets.chomp print "How much? "

尝试运行此代码

运行method1时,将返回两次散列,这意味着将按照“puts method1().inspect”命令的预期返回和打印散列

当method2运行时,第二次退出循环时,通过键入“no”或“n”,将打印一组看似随机的数字,而不是一个可爱的散列。这是为什么

def method1

    loop do
        print "Item name: "
        item_name = gets.chomp

        print "How much? "
        quantity = gets.chomp.to_i

        hash = {"Item"=> item_name, "quantity"=> quantity}
        puts hash.inspect

        return hash
    end

end

puts method1().inspect


def method2

    loop do
        print "Item name: "
        item_name = gets.chomp

        print "How much? "
        quantity = gets.chomp.to_i

        hash = {"Item"=> item_name, "quantity"=> quantity}
        puts hash.inspect

        print "Add another item? "
        answer = gets.chomp.downcase
        break if (answer == "no") || (answer == "n")
    end

    return hash

end

puts method2().inspect

你不小心发现了这个方法。您没有在循环外声明
散列
,因此它不在最后返回的范围内。相反,它返回
hash()
method值,这对于该实例来说是一个很大的负数

启动irb,然后键入hash,您将看到相同的结果:

(505)⚡️ irb
2.1.2 :001 > hash
 => -603961634927157790 
因此,请尝试以下方法:

def method2
    hash = {}
    loop do
        # ...

还要注意,您没有添加到哈希,每次都在创建它。

method2
中,您试图返回超出范围的内容(
hash

method1
中,您仍然处于循环中,在返回时定义了
hash
。在
method2
中,您不在定义
hash
的范围内,因此它具有未记录的结果。重新定义
方法2
如下:

def method2
    hash = nil

    loop do
        print "Item name: "
        item_name = gets.chomp

        print "How much? "
        quantity = gets.chomp.to_i

        hash = {"Item"=> item_name, "quantity"=> quantity}
        puts hash.inspect

        print "Add another item? "
        answer = gets.chomp.downcase
        break if (answer == "no") || (answer == "n")
    end

    return hash

end

现在,即使最初将
hash
设置为nil,它的范围包括整个方法,并且值将被保留;循环中的
散列
仅对该块可见。如果您使用的变量名还不是一个方法,那么您将得到一个非常不同的结果:)也不是问题的答案,只是一个注释。您的
hash
变量将只保存最后一个结果,因为您正在使用
hash=
在每个循环上覆盖它。查看
Hash#merge
您可能希望修复此方法以实际附加到哈希中,因为这似乎是OP想要的,但OP似乎误解了赋值的工作方式。