Prefer using then over yield_self

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: ruby-code-style/yield-self-to-then

Language: Ruby

Severity: Notice

Category: Best Practices

Description

The rule “Prefer using then over yield_self” is a coding practice in Ruby that helps improve code readability and simplicity. In Ruby, both yield_self and then are used to yield the receiver to a block and return the result. However, as of Ruby 2.6, then is the preferred method due to its simplicity and clearer syntax.

The importance of this rule lies in the maintenance and readability of the code. then is more intuitive and easier to understand for developers, especially those who are new to Ruby. This can lead to fewer misunderstandings and bugs in the code, and make it easier for developers to read and maintain the code in the future.

To follow this rule, replace any instances of yield_self with then. For example, instead of writing foo.yield_self { |x| x.do_something }, you should write foo.then { |x| x.do_something }. Similarly, "FOO".yield_self { |x| x.downcase } should be replaced with "FOO".then { |x| x.downcase }.

Non-Compliant Code Examples

foo.yield_self { |x| x.do_something }
"FOO".yield_self { |x| x.downcase }

Compliant Code Examples

foo.then { |x| x.do_something }
"FOO".then { |x| x.downcase }
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: esther/docs-9518-update-example-control-sensitive-log-data