Created
March 8, 2018 14:17
-
-
Save externvoid/f8c7420547449c5afa412d1e988bffaa to your computer and use it in GitHub Desktop.
クラスメソッド、得意メソッドの作り方
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 得意メソッド、クラスメソッド定義2連発、3連発 | |
| # class << C, class << a, def self.foo, def a.foo | |
| class C;end | |
| # (1) | |
| class << C | |
| def foo | |
| puts 'OK' | |
| end | |
| end | |
| class C | |
| foo | |
| end | |
| class C | |
| # (2) | |
| def self.bar | |
| puts 'OK bar' | |
| end | |
| end | |
| C.bar | |
| class C | |
| # (3) | |
| class << self | |
| def baz | |
| puts 'OK baz' | |
| end | |
| end | |
| end | |
| C.baz | |
| # ------- | |
| a = "OK" | |
| # [1] | |
| def a.foo | |
| puts 'OK foo' | |
| end | |
| a.foo | |
| class << a | |
| # [2] | |
| def bar | |
| puts 'Or bar' | |
| end | |
| end | |
| a.bar |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment