在IRB中编写一个简单的条件语句
我正在查找 url 为“http://”的所有组织,并将其属性更新为“”。
我的尝试:
Organization(:all).select { |o| o.url = "http://" ? o.update_attribute("url","")}
返回编译错误:
SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
from (irb):2
有什么想法吗?
I am looking up all Organizations with the url "http://", and updating their attributes to "".
My attempt:
Organization(:all).select { |o| o.url = "http://" ? o.update_attribute("url","")}
Which returns a compile error:
SyntaxError: compile error
(irb):2: syntax error, unexpected '}'
from (irb):2
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用
update_all
Try using
update_all
我绝不是红宝石专家,但我的第一个怀疑是您使用的是赋值运算符 (=) 而不是等于运算符 (==)。谷歌快速搜索“ruby irb conditional”似乎证明了这一点。
您可能会投反对票,因为您的问题中没有包含编译错误。如果我的猜测是错误的,我什至无法帮助尝试解释错误消息,因为你没有提供它。
更新:根据对答案的第一条评论,我相信我的第一个怀疑是错误的 - 对代码行意图的误解。但是,当错误消息不可用时,就会发生这种情况。
UPDATE2:第一条评论不存在...也许它被删除了或者也许我开始在错误的地方输入...
I'm by no means a ruby expert, but my first suspicion is that you're using an assignment operator (=) instead of an equality operator (==). A quick google search for "ruby irb conditional" appears to prove this.
And you probably got the down vote because you did not include compilation errors in your question. If my guess is wrong, I can't even help try to interpret the error message, because you didn't provide it.
UPDATE: based on the first comment to the answer, I believe my first suspicion to be wrong - a misinterpretation of the intent of the line of code. But, then, this is what happens when error messages are not availble.
UPDATE2: first comment not there... maybe it was deleted or maybe I started typing in the wrong place...