Ruby Style Guide (rubystyle.guide) conventions. Use when writing, formatting, or reviewing Ruby code for layout, naming, flow of control, methods, classes, and idioms. Complements RuboCop.
This skill applies the Ruby Style Guide — the community style guide that RuboCop is based on. Use when writing or reviewing Ruby for consistency and readability.
"Programs must be written for people to read, and only incidentally for machines to execute."
Consistency within a project matters more than strict adherence; when in doubt, match surrounding code.
; to terminate statements.puts 'a', 'b').sum = 1 + 2). No space after !, inside range literals (1..3), or after {/before } in interpolations.&. chains; prefer explicit checks or delegation.private/protected; none around method/class/module bodies.. or trailing . consistently (leading . preferred: continue on next line with .method).snake_case for symbols, methods, variables. CapitalCase for classes and modules (e.g. SomeClass, SomeXML). SCREAMING_SNAKE_CASE for other constants.snake_case (e.g. hello_world.rb). One class per file; filename mirrors class name.? (e.g. empty?). Avoid prefixing with is_, does_, can_.! only when a "safe" counterpart exists (e.g. update! vs update)._ (e.g. _unused_var, or |_k, v| in blocks).for. Use each etc., not for elem in arr.then for multi-line if/unless/when.result = condition ? a : b. Avoid nested ternaries; use if/else.case over if/elsif when comparing the same value. Indent when as deep as case (outdented).! instead of not. Avoid double negation !! unless you need an explicit boolean.&&/|| in conditions. Reserve and/or for control flow (e.g. raise or return) if at all.do_something if condition. Avoid modifier at end of long blocks.unless over if !. Don’t use else with unless; rewrite with positive case first.if/while/unless.loop do with break over while true or begin...end while.return where the last expression is the return value.self except for writers (self.foo =), reserved-word method names, or overloaded operators.next over deep nesting. Use guard clauses to bail on invalid input.raise over fail. Use two-arg form: raise SomeException, 'message'. Don’t specify RuntimeError explicitly in two-arg raise.ensure. Use implicit begin (rescue in method). Don’t suppress exceptions; handle or re-raise. Don’t use exceptions for flow of control.StandardError, IOError), not bare Exception. Put more specific rescues first.def foo(bar:, baz: false)).def with no params; use them when there are params. Omit for method calls with no args and for "keyword" methods (e.g. puts, raise) per project convention.... for argument forwarding when delegating (Ruby 2.7+). Use & for block forwarding (Ruby 3.1+).def self., initialize, public methods, then protected/private.include Foo and include Bar, not include Foo, Bar.module_function for namespaces of class methods over classes with only self. methods.@@); use class instance variables or dependency injection.private/protected; indent them like method definitions with one blank line above and below.to_s for domain objects. Use attr_reader/attr_accessor; avoid attr. Prefer Struct.new or Data.define for simple value holders (don’t extend them).#. Capitalize and punctuate full sentences.KEYWORD: note (e.g. TODO:, FIXME:, OPTIMIZE:, HACK:, REVIEW:).[] and {} for literals. Prefer %w[]/%i[] for word/symbol arrays. Prefer key: over :key => in hashes.Hash#key?/value? not has_key?/has_value?. Use fetch for required keys; use block form for expensive defaults.map/find/select/reduce/include?/size over collect/detect/find_all/inject/member?/length where it helps readability.any?/none?/one? instead of count > 0 etc. Don’t use count when size/length is enough (e.g. for Hash)."#{a} #{b}". Use single quotes when no interpolation/special chars. Use << for building large strings. Use \A and \z for full-string regex (not ^/$).require_relative for internal code; require for external libs. Omit .rb in require/require_relative.predicate? and Comparable#between? over manual comparisons where it clarifies intent.public_send over send; prefer __send__ if the name might be overridden.