Ruby块语法错误 [英] Ruby Block Syntax Error
问题描述
可能重复:
Ruby块和未括号的参数
Possible Duplicate:
Ruby block and unparenthesized arguments
我不确定我是否理解此语法错误.我正在使用Carrierwave在Rails应用程序中管理一些文件上传,而且我似乎错误地将一个块传递给其中一种方法.
I'm not sure I understand this syntax error. I'm using Carrierwave to manage some file uploads in a Rails app, and I seem to be passing a block to one of the methods incorrectly.
以下是载波文档中的示例:
version :thumb do
process :resize_to_fill => [200,200]
end
这就是我所拥有的:
version :full { process(:resize_to_limit => [960, 960]) }
version :half { process(:resize_to_limit => [470, 470]) }
version :third { process(:resize_to_limit => [306, 306]) }
version :fourth { process(:resize_to_limit => [176, 176]) }
以上操作无效,我得到了syntax error, unexpected '}', expecting keyword_end
.有趣的是,以下内容可以完美运行:
The above doesn't work, I get syntax error, unexpected '}', expecting keyword_end
. Interestingly enough, the following works perfectly:
version :full do process :resize_to_limit => [960, 960]; end
version :half do process :resize_to_limit => [470, 470]; end
version :third do process :resize_to_limit => [306, 306]; end
version :fourth do process :resize_to_limit => [176, 176]; end
所以,我的问题是,为什么在这种情况下我可以使用do...end
传递一个块而不是大括号?
So, my question is, why can I pass a block using do...end
but not braces in this instance?
谢谢!
推荐答案
尝试一下:
version(:full) { process(:resize_to_limit => [960, 960]) }
version(:half) { process(:resize_to_limit => [470, 470]) }
version(:third) { process(:resize_to_limit => [306, 306]) }
version(:fourth) { process(:resize_to_limit => [176, 176]) }
您有优先权问题. { }
块的绑定比do...end
块更紧密,比方法调用更紧密;结果是Ruby认为您正在尝试提供一个块作为符号的参数,而拒绝.
You have a precedence problem. The { }
block binds tighter than a do...end
block and tighter than a method call; the result is that Ruby thinks you're trying to supply a block as an argument to a symbol and says no.
通过比较以下内容,您可以看到一个更清晰的(?)或更熟悉的示例:
You can see a clearer (?) or possibly more familar example by comparing the following:
[1, 2, 3].inject 0 { |x, y| x + y }
[1, 2, 3].inject(0) { |x, y| x + y }
这篇关于Ruby块语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!