Skip to content

Instantly share code, notes, and snippets.

@mkurtikov
Last active April 12, 2016 06:09
Show Gist options
  • Save mkurtikov/08c73bc6dfd4d5ec316bd319736ec2e2 to your computer and use it in GitHub Desktop.
Save mkurtikov/08c73bc6dfd4d5ec316bd319736ec2e2 to your computer and use it in GitHub Desktop.
Advancep OOP
A = Class.new do
attr_reader :a
def initialize(a)
@a = a
end
end
a = A.new 1
a.class.class #class
# 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
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
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