Rails 控制台的奇怪行为
当我在 Rails 3.0.1 控制台中运行多行语句时,按 Enter 键实际上并不运行该语句。相反,它会转到一个新的控制台行,并且光标已向右移动。然后我必须运行一个基本行(如 p "hey"
),然后多行语句将运行。
ruby-1.9.2-p0 > images = Image.all;images.each do |im|; if im.imagestore_width.blank?;im.save;end;
ruby-1.9.2-p0 > p "hey"
我这样做已经有一段时间了,而且效果很好。但现在我在控制台中遇到了问题,它可能是相关的。当我运行上面的代码时,它并没有像平常那样工作,而是转到一个带有 ? 的新控制台行。添加
ruby-1.9.2-p0 > images = Image.all;images.each do |im|; if im.imagestore_width.blank?;im.save;end;
ruby-1.9.2-p0 > p "hey"
ruby-1.9.2-p0 ?>
当执行此操作时,我无法退出控制台
ruby-1.9.2-p0 ?> exit
ruby-1.9.2-p0 ?> ^C
这些问题是否相关?我该如何修复它们?
When I run a multi-line statement in the Rails 3.0.1 console, pressing enter doesn't actually run the statement. Instead, it goes to a new console line, and the cursor has been tabbed to the right. Then I have to run a basic line (like p "hey"
), and then the multi-line statement will run.
ruby-1.9.2-p0 > images = Image.all;images.each do |im|; if im.imagestore_width.blank?;im.save;end;
ruby-1.9.2-p0 > p "hey"
I've been doing it like this for awhile and it's been working okay. But now I've got a problem in the console and it might be related. When I ran the above code, instead of it working like it normally does, it just went to a new console line with a ? added
ruby-1.9.2-p0 > images = Image.all;images.each do |im|; if im.imagestore_width.blank?;im.save;end;
ruby-1.9.2-p0 > p "hey"
ruby-1.9.2-p0 ?>
When it does this, I can't exit the console
ruby-1.9.2-p0 ?> exit
ruby-1.9.2-p0 ?> ^C
Are these problems related? How can I fix them?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在该行中:
您有一个
end
来关闭if
,但没有end
来关闭do
块每个
。这就是为什么控制台在执行语句之前重新显示提示,要求输入更多信息。
尝试:
注意,您将看到带有括号的相同行为。
irb
或console
在括号平衡之前不会执行,例如In the line:
You have an
end
to close theif
but not anend
to close thedo
block of theeach
.This is why the console is redisplaying the prompt asking for more input before executing your statements.
Try:
Notice, you will see the same behaviour with brackets.
irb
orconsole
won't execute until the brackets balance e.g.不知道 irb/console 有什么问题,但你的 ruby 代码可能看起来更好:
普遍的共识是在 ruby 中使用 {} 而不是 do/end 来处理单行块。
Dunno what's wrong with irb/console but your ruby code could look a lot nicer:
The general consensus is to use {} rather than do/end for single line blocks in ruby.