コンテキストに応じたメソッドの動作の変更

基本的にオタク向けのパズル:
クラスAがあり、1つのパラメーターを受け取るsaveメソッドがあり、パラメーターにはデフォルト値があります(trueとしましょう)。 パラメーターなしでsaveメソッドを受け入れるクラスAのインスタンスは、自動的にtrueを受け入れ、このフラグに従って動作します。 タイプAフィールドを持つクラスBがあります。パラメーターなしでクラスBのコンテキスト内からこのフィールドのsaveメソッドを呼び出す場合、制御はfalseパラメーターのデフォルト値を取る別のメソッド実装に転送する必要があります。 オーバーライドの仕組みについてですが、グローバルではなく、1つのクラス内のみです。


空白は次のとおりです。
Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  1. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  2. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  3. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  4. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  5. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  6. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  7. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  8. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  9. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  10. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  11. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  12. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  13. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  14. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  15. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  16. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"
  17. Copy Source | Copy HTML class A def save (validate = true ) validate ? "INVALID" : "VALID" end end class B # - def initialize @a = A .new end def save @a. save end end A . new . save # => "INVALID" B . new . save # => "VALID" A . new . save # => "INVALID"


Source: https://habr.com/ru/post/J89058/


All Articles