Last active
April 12, 2016 06:09
-
-
Save mkurtikov/08c73bc6dfd4d5ec316bd319736ec2e2 to your computer and use it in GitHub Desktop.
Advancep OOP
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
| A = Class.new do | |
| attr_reader :a | |
| def initialize(a) | |
| @a = a | |
| end | |
| end | |
| a = A.new 1 | |
| a.class.class #class |
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
| # http://www.integralist.co.uk/posts/eigenclass.html | |
| class B | |
| def self.eigenclass | |
| class << self | |
| self | |
| end | |
| end | |
| def self.b | |
| p 'B' | |
| end | |
| end | |
| B.instance_methods.include? :b # false | |
| B.methods.include? :b # true | |
| B.eigenclass.instance_methods.include? :b # true | |
| B.class # Class | |
| B.superclass # Object | |
| B.eigenclass.class # Class | |
| B.eigenclass.superclass # <Class:Object> | |
| B.eigenclass.new # TypeError: can't create instance of singleton class |
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
| class B | |
| def eigenclass | |
| class << self | |
| self | |
| end | |
| end | |
| end | |
| b = B.new | |
| b.eigenclass # <Class:#<B:0x007f99130465d8>> | |
| b.eigenclass.superclass # B | |
| b.eigenclass.equal? B.new.eigenclass # false | |
| def b.b | |
| p 'b' | |
| end | |
| b.eigenclass.instance_methods.include? :b # true | |
| B.new.eigenclass.instance_methods.include? :b # false |
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
| class A | |
| def self.b | |
| p @@b | |
| end | |
| def self.b=(b) | |
| @@b = b | |
| end | |
| def self.a | |
| p @a | |
| end | |
| def self.a=(a) | |
| @a = a | |
| end | |
| end | |
| class B < A | |
| end | |
| A.b = 1 | |
| A.b # 1 | |
| B.b = 2 | |
| B.b # 2 | |
| A.b # 2 | |
| A.a = 1 | |
| B.a = 2 | |
| A.a # 1 | |
| B.a # 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment