"created", "whatCreated" => "thing", "htmlOutput" => -6ren">
gpt4 book ai didi

ruby - 为什么空格会影响 ruby​​ 函数调用?

转载 作者:数据小太阳 更新时间:2023-10-29 07:10:02 27 4
gpt4 key购买 nike

这段代码有语法错误

render json: {
"what" => "created",
"whatCreated" => "thing",
"htmlOutput" => render_to_string (partial: "some_partial")
}

但是对于这段代码我没有:

render json: {
"what" => "created",
"whatCreated" => "thing",
"htmlOutput" => render_to_string(partial: "some_partial")
}

为什么 render_to_string 之后的空间会破坏我的 Rails 应用程序?

最佳答案

问题是,ruby 中的方法可以带或不带括号运行。例如,您可以运行 Array.new 1,2 并且 ruby​​ 知道它在空格后接收参数。您还可以运行 Array.new(1,2) 并且 ruby​​ 知道参数在括号内。

但是,当您运行 Array.new (1,2) 时,ruby 认为它会在空格后接收参数,但实际上它接收的是一个元组 (1,2),基本上它与 Array.new((1,2))

完全相同

所以底线:

Array.new (1,2) == Array.new((1,2)) 这是一个语法错误,因为 (1, 2 ) 文字不是有效的

关于ruby - 为什么空格会影响 ruby​​ 函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26480823/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com