Rails 3 - 重构 ruby​​ 条件

发布于 2024-11-02 10:31:40 字数 247 浏览 0 评论 0原文

我想知道是否有更简单的方法可以在 ruby​​ 中执行这两个条件:

if params[:action] == 'index' || params[:action] == 'show'

if !(comment = (session[:my_params].include?(:comment) rescue nil)).nil?

提前致谢

I'd like to know if there is a simpler way to do these 2 conditions in ruby :

if params[:action] == 'index' || params[:action] == 'show'

and

if !(comment = (session[:my_params].include?(:comment) rescue nil)).nil?

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浪推晚风 2024-11-09 10:31:40

对于第一行,您可以这样做:

if %w(index show).include?(params[:action])

第二行实际上应该重构为两行:条件检查中的赋值是代码味道;从来没有理由。

如果您使用 Rails / ActiveSupport,则可以利用 Object#try

comment = session[:my_params].try(:include?, :comment)
if comment
  # ... comment is in scope
end

否则,您会遇到一些稍微笨拙的东西:

comment = session[:my_params].include?(:comment) rescue nil
if comment
  # etc

For the first one, you could do:

if %w(index show).include?(params[:action])

The second should really be re-factored into two lines: assignment within a condition check is a code smell; there's never a reason for it.

If you're using Rails / ActiveSupport, you can take advantage of Object#try

comment = session[:my_params].try(:include?, :comment)
if comment
  # ... comment is in scope
end

Otherwise, you're left with something slightly clunkier:

comment = session[:my_params].include?(:comment) rescue nil
if comment
  # etc
迷鸟归林 2024-11-09 10:31:40

1:

if ['index','show'].include? params[:action]

2:

if (comment = (session[:my_params].include?(:comment) rescue nil))

第二个条件中的 !.nil? 是多余的

但是,实际上,您不应该尝试使所有内容尽可能短,这是首先要关心的是你的代码对其他人来说有多清晰。第二个条件应该是这样的:

if ( comment = (session[:my_params] && session[:my_params].include?(:comment) )

或者甚至

comment = session[:my_params] && session[:my_params].include?(:comment)
if comment 

1:

if ['index','show'].include? params[:action]

2:

if (comment = (session[:my_params].include?(:comment) rescue nil))

! and .nil? in second condition are redundant

But, really, you should not try to make everything as short as possible, the first thing to care about is how clear your code would be for other people. The second condition should look like:

if ( comment = (session[:my_params] && session[:my_params].include?(:comment) )

or even

comment = session[:my_params] && session[:my_params].include?(:comment)
if comment 
情绪 2024-11-09 10:31:40

第一个可以像这样折射:

if ['index', 'show'].include? params[:action]

if %w(index show).include? params[:action]

First one can be refractored like this:

if ['index', 'show'].include? params[:action]

or

if %w(index show).include? params[:action]
冷月断魂刀 2024-11-09 10:31:40

这应该比使用数组和 include? 更快:

case params[:action]; when 'index', 'show'
   ...
end

第二个:

if comment = (session.fetch(:my_params, {}).include?(:comment))

This should be faster than using an array and include?:

case params[:action]; when 'index', 'show'
   ...
end

The second one:

if comment = (session.fetch(:my_params, {}).include?(:comment))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文