このページは日本語には対応しておりません。随時翻訳に取り組んでいます。翻訳に関してご質問やご意見ございましたら、お気軽にご連絡ください。

Metadata

ID: ruby-best-practices/prevent-attr

Language: Ruby

Severity: Info

Category: Best Practices

Description

The attr method in Ruby is an older method that was commonly used before attr_accessor, attr_reader, and attr_writer were introduced. It is now considered best practice to use these newer methods instead of attr for better clarity and consistency in your code.

The attr method can lead to confusion because it behaves differently depending on the number and type of arguments passed to it. If it is passed one argument, it behaves like attr_reader, creating a getter method for an instance variable. If it is passed two arguments, with the second argument being true, it behaves like attr_accessor, creating both a getter and a setter method. This can lead to unexpected behavior if not used carefully.

To adhere to this rule and avoid confusion, always use attr_accessor to create both getter and setter methods, attr_reader to create only getter methods, and attr_writer to create only setter methods. This will make your code more readable and maintainable. Avoid using attr altogether. For example, instead of writing attr :something, true, write attr_accessor :something. Instead of attr :one, :two, :three, write attr_reader :one, :two, :three.

Non-Compliant Code Examples

class Something
    attr :something, true
    attr :one, :two, :three
end

Compliant Code Examples

class Something
    attr_accessor :something
    attr_reader :one, :two, :three
end
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