unless scope.nil?
@page ||= Page.find(id, :scope => Page.find(scope))
else
@page ||= Page.find(id)
end
0
A
回答
3
@page ||= Page.find id, :scope => (Page.find scope if scope)
+0
这当然是最好的了,正如最后提到的那样,它当然是优先考虑的。 – ktec 2010-05-11 15:38:01
0
你可以这样做:
@page ||= unless scope.nil?
Page.find(id, :scope => Page.find(scope))
else
Page.find(id)
end
0
或者:
@page ||= scope.nil? ? Page.find(id) : Page.find(id, :scope => Page.find(scope))
1
我会写像以下问题的块。它真的归结为偏好,但我觉得这种方式是最可读的。
@page ||=
if scope
Page.find id, :scope => Page.find(scope)
else
Page.find id
end
2
这是一个位机:
find_opts = scope.nil? ? {} : {:scope => Page.find(scope)}
@page ||= Page.find(id, find_opts)
相关问题
- 1. 我该如何重构这个简单的Ruby代码片段?
- 2. 我该如何重构Ruby的这段代码?
- 3. 我该如何干这段代码?
- 4. 我该如何重复这段代码?
- 5. 我该如何重构这段代码?
- 6. 我该如何测试这段代码?
- 7. 我该如何改进这段代码?
- 8. 我该如何运行这段代码?
- 9. 我该如何循环这段代码?
- 10. 如何让这段代码更多Ruby方式?
- 11. 我该如何解决这个Ruby是/否风格循环?
- 12. 我应该如何格式化这段代码?
- 13. 我该如何重构这段代码才能更简洁?
- 14. 我该如何重构/使这段代码更pythonic?
- 15. 我该如何让这段代码更简单?
- 16. 如何干这段Ruby代码?
- 17. 这段代码应该如何缩进?
- 18. 我该如何使这段代码更漂亮/更好/更少线条?
- 19. 这是什么风格的装配(intel,att ...等),我该如何制作它?
- 20. 我该如何修改这段代码来代替javascript函数?
- 21. 我应该重构这段代码吗?
- 22. 我该如何制作'csgocrash'风格的图表
- 23. 我该如何重构这个ruby代码呢?
- 24. 我该如何正确放置这个ruby代码?
- 25. 我该如何强制我的代码分段错误?
- 26. 这个Fibonacci代码(Ruby)如何工作?
- 27. FormsAuthenticationModule中的这段代码应该如何工作?
- 28. 我该如何改进用Scala编写的这段代码?
- 29. 我该如何修复wordpress主题的这段代码
- 30. 我该如何延迟这段特定的代码?
在未来,请四个空格等堆栈溢出所示,当它实际上看起来像'code'缩进代码。 – 2010-05-07 14:36:41