Ruby’s Anonymous Eigenclass: Putting the “Ei” in Team

Classes

Hash.class = Class
Array.class = Class
Symbol.class = Class
Integer.class = Class
Class.class = Class
self
=>main
self.class
=>Object

Back to the Content!

class << self      
self
end
def eigenclass    
class << self
self
end
end
class Thing
def eigenclass
class << self
self
end
end
end
thing1 = Thing.new
=> #<Thing:0x007fb1a99224c8>
thing1.eigenclass
=>#<Class:#<Thing:0x007fb1a99224c8>>
thing1.eigenclass.class
=> Class
thing1.eigenclass.superclass
=> Thing
thing2 = Thing.new
=> #<Thing:0x007fb1aa81d860>
thing2.eigenclass
=> #<Class:#<Thing:0x007fb1aa81d860>>
thing2.eigenclass != thing1.eigenclass
=> true
class Thing
def eigenclass
class << self
self
end
end
def self.what_am_i?
"I'm a thing!"
end
end
Thing.what_am_i?
=> "I'm a thing!"
Thing.what_am_i?
=> "I'm a thing!"
Thing.singleton_methods
=> [:what_am_i?]
Thing.methods
=> [:what_am_i?, :new, :allocate, :superclass, :<=>, :module_exec, :class_exec, :<=, :>=, :==, :===, :include?, :included_modules, :ancestors, :name, :public_instance_methods, :instance_methods, :private_instance_methods, :protected_instance_methods, :const_get, :constants, :const_defined?, :const_set, :class_variables, :class_variable_get, :remove_class_variable, :class_variable_defined?, :class_variable_set, :private_constant, :public_constant, :singleton_class?, :deprecate_constant, :freeze, :inspect, :module_eval, :const_missing, :prepend, :method_defined?, :class_eval, :public_method_defined?, :private_method_defined?, :<, :public_class_method, :>, :protected_method_defined?, :private_class_method, :to_s, :autoload, :autoload?, :instance_method, :public_instance_method, :include, :lm, :lim, :instance_of?, :public_send, :instance_variable_get, :instance_variable_set, :instance_variable_defined?, :remove_instance_variable, :private_methods, :kind_of?, :instance_variables, :tap, :define_singleton_method, :is_a?, :public_method, :extend, :singleton_method, :to_enum, :enum_for, :=~, :!~, :eql?, :respond_to?, :display, :object_id, :send, :method, :nil?, :hash, :class, :singleton_class, :clone, :dup, :itself, :taint, :tainted?, :untaint, :untrust, :trust, :untrusted?, :methods, :protected_methods, :frozen?, :public_methods, :singleton_methods, :!, :!=, :__send__, :equal?, :instance_eval, :instance_exec, :__id__]
class Foo
def self.bar
p "I'm public"
end

private
def self.baz
p "I'm private"
end
end

Foo.bar # => I'm public
Foo.baz # => I'm private
class Foo
def self.bar
puts "I'm public"
end
class << self
private
def baz
"I'm private"
end
end
end
Foo.bar # => I'm public
Foo.baz # => NoMethodError: private method `baz' called for Foo:Class
class Foo
def self.bar
p "I'm public"
end
@baz = "I'm a class variable!" class << self
attr_accessor :baz
end
end
Foo.bar
=> "I'm public"
Foo::baz
=>"I'm a class variable!"

BONUS:

class Foo
def self.bar
p "im public"
end

def self.baz
p "im private"
end

private_class_method :baz
end

Foo.bar # => im public
Foo.baz # => NoMethodError: private method `baz' called for Foo:Class

--

--

--

Full-Stack Software Engineer | NYC

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

P2P Container Image Distribution on IPFS With Containerd

Boost your productivity with this JupyterLab Hack!

EBOOK [P.D.F] Next Generation Java Testing: TestNG and Advanced Concepts Full-Online

RAII in C++

Why Automate Tests and Where to Start ? ~ Part-1

Space to connect

How to set up ssh key pair with ubuntu

A case study of contributing to a Terraform Provider

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ethan R. Roberts

Ethan R. Roberts

Full-Stack Software Engineer | NYC

More from Medium

CodeWars Fundamentals: Remove the Minimum

Free React course for beginners to support Ukraine

6 Algorithms Every Developer Should Know

How Outsourcing Complements Flatter Organizational Structures

How Outsourcing Complements Flatter Organizational Structures