Ruby's object model has the following key points:
1. Classes are objects in Ruby and are open for modification and extension.
2. The Kernel module includes common methods like Array and Hash that can be called without an explicit receiver.
3. Classes inherit from other classes, not individual objects, and share methods but not instance variables.
4. Modules allow grouping of methods for inclusion in classes as an alternative to multiple inheritance.
6. What is Kernel???
Kernel is a module which is included in the Object. Where These Method can be Call Without the
receiver and functional Form.
Eg. Array(1..5)
Hash([1,2,3])
7. Classes
● Approach in Ruby is different having all the classes are open.
● Overview
Problem: Make a method for all the type of string which convert it to
alphanumeric string includes special character etc ?
How To Do that ???????
Does Open Class will Help ??????????
If Yes then How ???????
8. Solution : Yes We can do it via Open Class of Ruby
class String
def to_alphanumeric
gsub /[^ws]/, ''
end
end
“Sys@@#tango”.to_alphanumeric => “Systango”
Open Class Problem ->
Revised method problem
9. Quiz Time
class MyClass < String.new
def self.to_s
puts “Class method”
end
end
MyClass.to_s
What is the output?
TypeError: Wrong argument type String (expected Class)
Lesson:
Classes inherit from classes only not from just any object
10. Points to Remember:
● Class inherit from the Type Class only not by the Type Object.
● Any Creating a New Class then it goes to the object because Class is a Object.
● Objects that share the same class also share the same methods, so the
methods must be stored in the class, not the object.
● An object’s instance variables live in the object itself,and an object’s methods live in the object’s
class. That’s why objects of the same class share methods but don’t share instance variables
String.instance_methods == "abc".methods # => true
String.methods == "abc".methods # => false
11. What is Self ??
● self is not the same as this in Java
● self is synonymous to current/default object
● Who gets to be self depends on Where self is
12. The joy of “self” discovery
p self
class MyClass
p self
def self.my_class_method
P self
end
def my_instance_method
P self
end
end
=> main (Which the the top level of hierarchy Object
Class)
=> MyClass (which is the type class)
=> MyClass (which is the type class explicit Reciever
is Class Name)
=> Instance of class (Object which is called this
method)
13. what is “main” in ruby?
puts self
class Foo
puts self
end
OUTPUT:
main
Foo
- Everything in Ruby occurs in the context of some object. The object at the top level is called "main". It's
basically an instance of Object with the special property that any methods defined there are added as
instance methods of Object (so they're available everywhere).
- Actually there is a main, but it is not a method; it's the top-level object that is the initial execution context
of a Ruby program.
14. Access modifiers
● Public
● Private : only accessible within the scope of a single object in which it is defined (truly private)
( Can’t call with an explicit receiver )
● Protected: accessible from the scope of a method belonging to any object that’s an instance of the
same class
(Invoked by Object of the defining class and it’s subclasses)
class MyClass
protected
def my_protected_method
end
public
def my_public_method(other)
self.my_protected_method
other.my_protected_method
end
end
#=> Works
#=> NoMethodError
mc1 = MyClass.new
mc2 = MyClass.new
mc1.my_public_method(mc2)
mc1.my_protected_method
15. Quiz Time
class MyClass
protected
def my_protected_method
end
def
self.my_protected_method
puts "Welcome"
end
public
def
my_public_method(other)
self.my_protected_method
other.my_protected_method
end
end
mc1 = MyClass.new
mc2 = MyClass.new
mc1.my_public_method(mc2)
MyClass.my_protected_method
=> Works
=> Works
16. Ruby Variable Scoping
● Class Variable (@@) - Available from the Class Definition and any Subclasses
● Instance Variable (@) - Specific Object, Across all the methods in a class Instance
● Global Variable($)
● Local Variable
=> Every Object has it’s own list of Instance Variable,Independent of other Objects- even other
objects of the Same Class.
17. Quiz Time
class A
y=1
@p = 2
@q
@@r = 2
def initialize
@@t=3
@s = 2
end
def welcome
@indore = 1
@bhopal = 2
@jabalpur = 3
@@dhar = 3
end
end
p A.instance_variables
p A.class_variables
a = A.new
b = A.new
p a.instance_variables
p A.class_variables
p b.welcome
p b.instance_variables
=> [:@p]
=> [:@@r]
=> [:@s]
=> [:@@r, :@@t]
=> [:@s, :@indore, :@bhopal, :@jabalpur]
18. Module
Module is just bunch of instance method with a couple of additional features ( A superClass and
New Method)
MyModule
->MyClass
-> MyConstant
->MyConstant
There are not same
Alternative to Multiple Inheritance
Code Examples:
module MyModule
def my_meth
puts “my_meth() of MyModule”
end
end
19. Case 1: Include MyModule instance methods as instance methods of myClass
class MyClass
include MyModule # Just include it…
end
20. Case 2: Include instance methods of MyModule as class methods of MyClass
21. Quiz Time
module MyModule
def self.my_freakin_meth
puts “my_freakin_meth() of MyModule”
end
end
class MyClass
include MyModule
end
MyClass.my_freakin_meth
What is the output?
#NoMethodError
Lesson:
When an object includes a module
Module’s instance methods are included
Module’s class methods are excluded
22. Golden Points
● Objects that share the same class also share the same methods, So the Method must
be stored in the Class, not the object.
● Object’s instance variable live in the Object itself and Object’s method live in the
Object’s Class.
● Methods of an object are also the instance method of it’s Class
Eg. String.instance_methods == “abc”.methods = > True
String.methods == “abc”.methods => False due to inclusion of private and protected
method.