rails rubocop 使用 && 而不是 render 和 return
rails rubocop use && instead of and with render and return
我已经在这个问题上停留了一段时间了,但一直跳过它。如果我遵循此处的警察指南并将其更改为 && 我的路线中断。它们通过以下方式连接到我的路线:
# Override Error Codes
match '/404', to: 'error#four_oh_four', via: :all
match '/422', to: 'error#four_twenty_two', via: :all
match '/500', to: 'error#five_hundred', via: :all
您是否建议针对此问题禁用此警察或是否有更好的调整方法?
class ErrorController < ApplicationController
before_filter :ensure_trailing_slash
before_action :load_log_service
def javascript_disabled
@log_service.capture_message(view_context.time_stamp('javascript_disabled'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.javascript_disabled')
end
def system_requirements
@log_service.capture_message(view_context.time_stamp('system_requirements'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.system_requirements')
end
def browser_upgrade_required
@log_service.capture_message(view_context.time_stamp('browser_upgrade_required'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.browser_upgrade_required')
end
def four_oh_four
@log_service.capture_message(view_context.time_stamp('four_oh_four'), 'error_code')
render_error_status_code('Page Not Found', '404', t('system_requirements.response_code.four_oh_four'))
end
# four_twenty_two, five_hundred :: no custom report needed, they are picked up by the sentry gem
def four_twenty_two
render_error_status_code('Application Error', '422', t('system_requirements.response_code.four_twenty_two'))
end
def five_hundred
render_error_status_code('Application Error', '500', t('system_requirements.response_code.five_hundred'))
end
private
def render_error_application_requirements(title)
@title = title
render 'errors/application_requirements', layout: 'errors/application_requirements' and return
end
def render_error_status_code(title, error_code, error_msg)
@title = title
@error_code = error_code
@error_msg = error_msg
render 'errors/status_code', layout: 'errors/status_code', status: @error_code and return
end
def load_log_service
@log_service = LogService.new
end
end
在你的Post下点个赞,确实不需要return
这里
但是,如果您仍想使用 return
并使其遵循 RuboCop 风格,请尝试用方括号包裹渲染参数。
render('errors/status_code', layout: 'errors/status_code', status: @error_code) && return
我认为您在此处对 render
调用的参数缺少括号让您感到困惑。 &&
的优先级高于 and
,区别在于改变了 Ruby 将参数标识为 render
.
的方式
虽然猜测并不好玩,但让我们确定一下。 MRI Ruby 包括 Ripper
,它允许我们检查表达式将如何被解析。
require 'ripper'
require 'pp'
和
Ripper.sexp("render 'errors/application_requirements', layout: 'errors/application_requirements' and return")
[:program,
[[:binary,
[:command,
[:@ident, "render", [1, 0]],
[:args_add_block,
[[:string_literal,
[:string_content, [:@tstring_content, "errors/status_code", [1, 8]]]],
[:bare_assoc_hash,
[[:assoc_new,
[:@label, "layout:", [1, 29]],
[:string_literal,
[:string_content,
[:@tstring_content, "errors/status_code", [1, 38]]]]],
[:assoc_new,
[:@label, "status:", [1, 59]],
[:var_ref, [:@ivar, "@error_code", [1, 67]]]]]]],
false]],
:and,
[:return0]]]]
&&
Ripper.sexp("render 'errors/application_requirements', layout: 'errors/application_requirements' && return")
[:program,
[[:command,
[:@ident, "render", [1, 0]],
[:args_add_block,
[[:string_literal,
[:string_content, [:@tstring_content, "errors/status_code", [1, 8]]]],
[:bare_assoc_hash,
[[:assoc_new,
[:@label, "layout:", [1, 29]],
[:string_literal,
[:string_content,
[:@tstring_content, "errors/status_code", [1, 38]]]]],
[:assoc_new,
[:@label, "status:", [1, 59]],
[:binary,
[:var_ref, [:@ivar, "@error_code", [1, 67]]],
:"&&",
[:return0]]]]]],
false]]]]
Ripper 的输出不是最容易阅读的东西,但如果我们密切注意那些函数调用的缩进,我们可以看到 and
案例正在调用 render(... '..._requirements') and return
而&&
个案例正在调用 render(... '..._requirements' && return)
。这两个表达式将具有非常不同的行为。
我已经在这个问题上停留了一段时间了,但一直跳过它。如果我遵循此处的警察指南并将其更改为 && 我的路线中断。它们通过以下方式连接到我的路线:
# Override Error Codes
match '/404', to: 'error#four_oh_four', via: :all
match '/422', to: 'error#four_twenty_two', via: :all
match '/500', to: 'error#five_hundred', via: :all
您是否建议针对此问题禁用此警察或是否有更好的调整方法?
class ErrorController < ApplicationController
before_filter :ensure_trailing_slash
before_action :load_log_service
def javascript_disabled
@log_service.capture_message(view_context.time_stamp('javascript_disabled'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.javascript_disabled')
end
def system_requirements
@log_service.capture_message(view_context.time_stamp('system_requirements'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.system_requirements')
end
def browser_upgrade_required
@log_service.capture_message(view_context.time_stamp('browser_upgrade_required'), 'error_scope')
render_error_application_requirements t('system_requirements.minimum_settings.browser_upgrade_required')
end
def four_oh_four
@log_service.capture_message(view_context.time_stamp('four_oh_four'), 'error_code')
render_error_status_code('Page Not Found', '404', t('system_requirements.response_code.four_oh_four'))
end
# four_twenty_two, five_hundred :: no custom report needed, they are picked up by the sentry gem
def four_twenty_two
render_error_status_code('Application Error', '422', t('system_requirements.response_code.four_twenty_two'))
end
def five_hundred
render_error_status_code('Application Error', '500', t('system_requirements.response_code.five_hundred'))
end
private
def render_error_application_requirements(title)
@title = title
render 'errors/application_requirements', layout: 'errors/application_requirements' and return
end
def render_error_status_code(title, error_code, error_msg)
@title = title
@error_code = error_code
@error_msg = error_msg
render 'errors/status_code', layout: 'errors/status_code', status: @error_code and return
end
def load_log_service
@log_service = LogService.new
end
end
在你的Post下点个赞,确实不需要return
这里
但是,如果您仍想使用 return
并使其遵循 RuboCop 风格,请尝试用方括号包裹渲染参数。
render('errors/status_code', layout: 'errors/status_code', status: @error_code) && return
我认为您在此处对 render
调用的参数缺少括号让您感到困惑。 &&
的优先级高于 and
,区别在于改变了 Ruby 将参数标识为 render
.
虽然猜测并不好玩,但让我们确定一下。 MRI Ruby 包括 Ripper
,它允许我们检查表达式将如何被解析。
require 'ripper'
require 'pp'
和
Ripper.sexp("render 'errors/application_requirements', layout: 'errors/application_requirements' and return")
[:program,
[[:binary,
[:command,
[:@ident, "render", [1, 0]],
[:args_add_block,
[[:string_literal,
[:string_content, [:@tstring_content, "errors/status_code", [1, 8]]]],
[:bare_assoc_hash,
[[:assoc_new,
[:@label, "layout:", [1, 29]],
[:string_literal,
[:string_content,
[:@tstring_content, "errors/status_code", [1, 38]]]]],
[:assoc_new,
[:@label, "status:", [1, 59]],
[:var_ref, [:@ivar, "@error_code", [1, 67]]]]]]],
false]],
:and,
[:return0]]]]
&&
Ripper.sexp("render 'errors/application_requirements', layout: 'errors/application_requirements' && return")
[:program,
[[:command,
[:@ident, "render", [1, 0]],
[:args_add_block,
[[:string_literal,
[:string_content, [:@tstring_content, "errors/status_code", [1, 8]]]],
[:bare_assoc_hash,
[[:assoc_new,
[:@label, "layout:", [1, 29]],
[:string_literal,
[:string_content,
[:@tstring_content, "errors/status_code", [1, 38]]]]],
[:assoc_new,
[:@label, "status:", [1, 59]],
[:binary,
[:var_ref, [:@ivar, "@error_code", [1, 67]]],
:"&&",
[:return0]]]]]],
false]]]]
Ripper 的输出不是最容易阅读的东西,但如果我们密切注意那些函数调用的缩进,我们可以看到 and
案例正在调用 render(... '..._requirements') and return
而&&
个案例正在调用 render(... '..._requirements' && return)
。这两个表达式将具有非常不同的行为。