当some_method的返回结果为nil时,我发现自己做了很多事情来防止nil。
a = a.some_method.present? ? a.some_method : []
Run Code Online (Sandbox Code Playgroud)
还有更红宝石的方法吗?我试过使用
a = []
a ||= a.some_method
Run Code Online (Sandbox Code Playgroud)
但是那当然会给我
a = []
Run Code Online (Sandbox Code Playgroud)
谢谢!