Skip to content
This repository was archived by the owner on Oct 21, 2025. It is now read-only.
Merged
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
25 changes: 25 additions & 0 deletions ja/ruby/standard.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ a, b = 1, 2
parameter_5, parameter_6, options)
```

* 長い文字列リテラルのせいで 80文字を越えてしまう場合のみ、80文字を越えることを許可する

```ruby
# ok
foo = "This is a very very long string that can't be broken down and may contain #{variable}"
```

* [] () {} 全ての前後にホワイトスペースは置かない

```ruby
Expand All @@ -58,6 +65,24 @@ a = [1, 2, 3] #"[" の左のスペースは = の後ろのもの
def method(a, b, c)
```

* 改行を含む長い文字列にはヒアドキュメントを使う。ただし、短いメッセージを定義する時や、メソッドチェーンしたいときには、文字列リテラルを使っても良い。
```ruby
# good
foo = <<-EOS
From this valley they say you are going,
We will miss your bright eyes and sweet smile,
For they say you are taking the sunshine
That has brightened our pathways a while.
EOS

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

うーん、これはダメ?

  foo = "yahoo!
greeting
  roi"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ヒアドキュメントのほうが高機能なので、両方の記法を許可するか、ヒアドキュメントのみに統一するべきだと思います。
その書き方のメリットが大きいのはどんなときでしょうか?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可読性が高く(と個人的に思ってます)、ヒアドキュメントよりもコード量が少ないので、短い文章であればこの方が早い場面もあるかな、と。
特に、ヘルパーでちょっとしたHTMLタグを出力する時とか。
統一するのであればヒアドキュメントという意見には賛成です。
これを禁止するかどうか、という点ですね。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    foo = "<div>
  <p>bar</p>
</div>"
    foo = <<-EOS
<div>
  <p>bar</p>
</div>
    EOS

上記のふたつのコードを比べると、後者のほうが出力される HTMLの構造が分かりやすいと思いますが、どうでしょうか。改行された HTML タグを出力したいときは、ほぼ間違いなくネストされた HTML タグを出力するときだと思います。
つまり、最初の行も含めて全体的にインデントが可読性に影響してくるものはヒアドキュメントが良いかと思います。

こういう場合なら、あまり大差ないと思いますが。

    foo = "Hello.
I'm Johnny."
    foo = <<-EOS
Hello.
I'm Johnny.
    EOS

あと、長い文字列の後でメソッドチェインしたいときは前者しかありませんね。

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(つまり、どちらについても、禁止すると不都合が出る場合がある、と。)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

あー、メソッドチェーンの視点は抜けてた。やりたい場面は少ないだろうけど、ありそうだね。
ってことで、ヒアドキュメント推奨として、

  • ごく短い改行を含むメッセージを定義する
  • メソッドチェーンしたい

等の場合は文字列リテラルでOK、ではどうですか?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

はい、それで良いと思います。修正しておきます。

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

メソッドチェーンできるんじゃないか?と思ったらできた。

<<EOS.capitalize
abc
  def
EOS
=> "Abc\n  def\n"

ってことで複数行の文字列リテラルは無しにしても良いかも。
これ踏まえてどう?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

思うにメソッドチェーンしたいのって、html_safe位かなーと思ってて、その時はJが言うようにヒアドキュメントだろうし。
全面廃止にできない理由は無くなったので、廃止しても良いかも、と思えてきました。


# ok
foo = "Hi, Johnny.
How are you?"

# bad
foo = "From this valley they say you are going,\nWe will miss your bright eyes and sweet smile,\nFor they say you are taking the sunshine\nThat has brightened our pathways a while."
```

* 仮引数の後にホワイトスペースを入れる

```ruby
Expand Down