📜 ⬆️ ⬇️

Changing the behavior of the method depending on the context

Essentially a problem for geeks:
There is a class A, it has a save method that takes one parameter, the parameter has a default value (say, true). An instance of class A, which accepts the save method with no parameters, automatically accepts true and works in accordance with this flag. There is class B, which has a field of type A. When you call the save method for this field from within the context of class B without parameters, control must be transferred to another implementation of the method, which accepts the default value of the parameter false. Approximately how override works, but not globally, but inside only one class.


Here is the stub:
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/89058/


All Articles