Classes are objects.
Class name can be assigned to a variable and used to create an instance.
irb
2.2.4 :001 > String.name
=> "String"
2.2.4 :002 > Class.name
=> "Class"
2.2.4 :003 > BasicObject.name
=> "BasicObject"
2.2.4 :004 > self.name
NoMethodError: undefined method `name' for main:Object
from (irb):4
from /Users/bparanj/.rvm/rubies/ruby-2.2.4/bin/irb:11:in `
2.2.4 :005 > self.to_s
=> "main"
String.to_s
=> "String"
2.2.4 :007 > Class.to_s
=> "Class"
2.2.4 :008 > BasicObject.to_s
=> "BasicObject"
Instance variables are stored in current instance.
In the middle of the class, the code is running.
Every method call has a receiver.
puts does not have explicit receiver.
Default object, is always the self (the current object)
We did not create any object. We ask main :
What is the class that is used to create an instance of main?
Methods are not objects. It can be converted to an object.
puts can be converted into an object and we can call puts.
What is the current object?
Example.
No compilation phase.
Definitions are active.
There is always a receiver.
Class definitions is executable.
Everything gets executed.
Instead of doing:
Calling an utility method:
Encryptor.encrypt('secret')
vs
'secret'.encrypt
Sending a message
module Some
end
Some.new
vs
Module.new
class Object
def to_s
'main'
end
end
class Person
puts self
puts self.class
end
class Person
def self.talk
end
end
class Person
def self.talk
end
Person.talk
end
class Person
def self.talk
end
self.talk
end
class Person
def self.talk
end
talk
end
class Person
def self.talk
end
end
Person.talk
class Dumbass < Person
talk
end