我通过了一个长期运行的遗留ruby程序,它有很多次出现
begin
#dosomething
rescue Exception => e
#halt the exception's progress
end
Run Code Online (Sandbox Code Playgroud)
贯穿始终.
如果不追踪每一个可能的异常,每个异常都可以处理(至少不是立即),我仍然希望能够有时关闭它CtrlC.
我想以一种只添加代码的方式这样做(所以我不会影响现有的行为,或者在运行过程中错过一个被捕获的异常.)
[ CtrlC是SIGINT,或SystemExit,它似乎与SignalException.new("INT")Ruby的异常处理系统相同.class SignalException < Exception,这就是为什么会出现这个问题的原因.
我想写的代码是:
begin
#dosomething
rescue SignalException => e
raise e
rescue Exception => e
#halt the exception's progress
end
Run Code Online (Sandbox Code Playgroud)
编辑:此代码有效,只要您获得要捕获正确的异常类.这是SystemExit,Interrupt或IRB :: Abort,如下所示.
我有一个关于Block的问题,这两个代码是否相同?
代码1
File::open('yozloy.txt','w') do |f|
f << 'Some contains'
end
Run Code Online (Sandbox Code Playgroud)
代码2
newFile = File::open('yozloy.txt','w')
newFile << 'Some contains'
Run Code Online (Sandbox Code Playgroud) 我已经安装了RVM以及ruby版本.但是,如果我启动控制台并运行命令rails服务器,捆绑安装等,我收到此错误
bash: /usr/bin/rails: /usr/bin/ruby1.8: bad interpreter: No such file or directory
Run Code Online (Sandbox Code Playgroud)
但如果我先跑rvm use 1.9.2,那一切都没问题.我尝试使用`rvm use --default 1.9.2',但没有改变.这是否意味着它使用与RVM中不同的ruby?提前致谢!
我查看了Arel源代码,以及Rails 3.0的一些activerecord源代码,但我似乎无法为自己收集一个很好的答案,因为在构造查询时Arel是否会改变我们使用includes()的能力, 为了更好.
有些情况下,人们可能想要修改activerecord上的条件:包括2.3.5及之前的查询,用于返回的关联记录.但据我所知,这对于所有人来说都不是以编程方式成立的:包含查询:
(我知道一些AR-find-includes make t#{n} .c#{m}重命名所有属性,可以想象为这些查询添加条件以限制连接集的结果;但是其他人做n_joins + 1个数字迭代地对id设置的查询,我不确定如何破解AR来编辑这些迭代查询.)
Will Arel允许我们构造ActiveRecord查询,在使用includes()时指定结果关联的模型对象?
例如:
User :has_many posts( has_many :comments)
User.all(:include => :posts) #say I wanted the post objects to have their
#comment counts loaded without adding a comment_count column to `posts`.
#At the post level, one could do so by:
posts_with_counts = Post.all(:select => 'posts.*, count(comments.id) as comment_count',
:joins => 'left outer join comments on comments.post_id = posts.id',
:group_by => 'posts.id') #i believe
#But it seems impossible to do so while …Run Code Online (Sandbox Code Playgroud)