Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ruby: Revise rule about braces #61

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -261,9 +261,7 @@ Style/WhileUntilModifier:

# [MUST] Do not put whitespace between a method name and parenthesis.

# [SHOULD] Omit `{ }` of a hash literal, when put on the end of an argument list.
Style/BracesAroundHashParameters:
EnforcedStyle: no_braces
# [SHOULD] Due to separation of keyword and positional arguments in Ruby 3.0, distinguish between using keyword arguments and using a hash in method calls.

# [MUST] Use `do`/`end` form for blocks of method calls where the return value of the block is unused. i.e. blocks executed for side-effects

Expand Down
20 changes: 16 additions & 4 deletions ruby.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -370,14 +370,26 @@ To ensure readability and consistency within the code, the guide presents a numb
p (1 + 2)
```

- **[SHOULD]** Omit `{ }` of a hash literal, when put on the end of an argument list.
- **[SHOULD]** Due to separation of keyword and positional arguments in Ruby 3.0, distinguish between using keyword arguments and using a hash in method calls.

```ruby
def foo(a:, b:)
p(a, b)
end

def bar(hash)
p(hash)
end

# good
foo(a: 1, b: 2)
# good
foo(1, 2, foo: :bar, baz: 42)
bar({a: 1, b: 2})

# bad
foo(1, 2, { foo: :bar, baz: 42 })
# bad - foo expects keyword arguments, but it passes a hash
foo({a: 1, b: 2})
# bad - bar expects a hash, but it passes keyword arguments
bar(a: 1, b: 2)
```

- **[MUST]** Use `do`/`end` form for blocks of method calls where the return value of the block is unused. i.e. blocks executed for side-effects
Expand Down
20 changes: 16 additions & 4 deletions ruby.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,14 +388,26 @@ Ruby プログラマとしての素養をある程度備えている者なら誰
p (1 + 2)
```

- **[SHOULD]** パラメータリストの末尾にハッシュリテラルを書く場合は、ハッシュリテラルの括弧を省略すること
- **[SHOULD]** Ruby 3.0 からのキーワード引数とポジショナル引数の分離を踏まえ、メソッド呼び出しではキーワード引数に渡しているのかハッシュオブジェクトを渡しているのかを区別する

```ruby
def foo(a:, b:)
p(a, b)
end

def bar(hash)
p(hash)
end

# good
foo(a: 1, b: 2)
# good
foo(1, 2, foo: :bar, baz: 42)
bar({a: 1, b: 2})

# bad
foo(1, 2, { foo: :bar, baz: 42 })
# bad - foo expects keyword arguments, but it passes a hash
foo({a: 1, b: 2})
# bad - bar expects a hash, but it passes keyword arguments
bar(a: 1, b: 2)
```

- **[MUST]** ブロック付きメソッド呼び出しでは、`do`/`end` 記法でブロックを書くこと。i.e. blockの副作用が目的
Expand Down