我有一些代码需要在ruby中拯救多种类型的异常:
begin
a = rand
if a > 0.5
raise FooException
else
raise BarException
end
rescue FooException, BarException
puts "rescued!"
end
Run Code Online (Sandbox Code Playgroud)
我想做的是以某种方式存储我想要在某处救援的异常类型列表,并将这些类型传递给rescue子句:
EXCEPTIONS = [FooException, BarException]
Run Code Online (Sandbox Code Playgroud)
然后:
rescue EXCEPTIONS
Run Code Online (Sandbox Code Playgroud)
这是否可能,并且可能没有一些真正的黑客调用eval
?鉴于我在TypeError: class or module required for rescue clause
尝试上述内容时所看到的情况,我并不抱希望.
我正在尝试获取一个21:00作为当地时间的NSDate对象 - 不关心哪一天.我对这个非常奇怪的结果感到头疼:
NSCalendar *calendar = [[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar];
NSDateComponents *components = [[NSDateComponents alloc] init];
[components setHour:21];
[components setMinute:0];
[components setSecond:0];
NSDate *date = [calendar dateFromComponents:components];
NSLog(@"%@", date);
Run Code Online (Sandbox Code Playgroud)
结果是 0001-01-02 04:52:58 +0000
我不知道为什么.当前时间是太平洋标准时间17:34,但结果不会随当地时间而变化.
如果我调整setMinute和setSecond行
[components setMinute:7];
[components setSecond:2];
Run Code Online (Sandbox Code Playgroud)
我知道0001-01-02 05:00:00 +0000
,这是正确的(太平洋标准时间21:00).