Skip to content

Instantly share code, notes, and snippets.

@k-tsj
Created June 16, 2025 13:44
Show Gist options
  • Save k-tsj/3d040228807fcf21452508371e026e57 to your computer and use it in GitHub Desktop.
Save k-tsj/3d040228807fcf21452508371e026e57 to your computer and use it in GitHub Desktop.

Q

以下のpower_assertの実行結果から読み取れることは何か?

assert { MyClass.foo.to_a.include?(3) }
                 |   |    |
                 |   |    false
                 |   [0, 1, 2]
                 #<MyClass>

A

評価の流れ:

  • MyClass はクラスオブジェクト。→ # と出ている。
  • MyClass.foo の結果は [0, 1, 2] という配列。
  • MyClass.foo.to_a はそのまま [0, 1, 2]。
  • include?(3) をこの配列に対して呼び出している。
  • [0, 1, 2].include?(3) は false。

Q

以下のpower_assertの実行結果から読み取れることは何か?

assert { MyClass.foo.to_a.include?(3) }
                 |   |    |
                 |   |    false
                 |   [0, 1, 2]
                 #<MyClass>

A

このpower_assertの実行結果から以下のことが読み取れます:

  1. MyClass.foo は [0, 1, 2] という配列を返している
  2. その配列に 3 が含まれているかチェックしたが、結果は false
  3. アサーションが失敗した理由は、配列 [0, 1, 2] に 3 が含まれていないため
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment