ruby object model - understanding of object play role for ruby
TRANSCRIPT
Ruby Object ModelThis is a game of object for Ruby
Ruby is a game but Syntax is not game.
This Theory helps to solve two biggest Questions
Which Class does any method come from ?
What happen when we include any specific module?
Ruby Objective
Key points :
Classes are Object.
Closed to modifications and Open to extend.
The Axiom
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])
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 ???????
Solution : Yes We can do it via Open Class of Ruby
class Stringdef to_alphanumeric
gsub /[^\w\s]/, ''endend
“Sys@@#tango”.to_alphanumeric => “Systango”
Open Class Problem ->
Revised method problem
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
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 # => trueString.methods == "abc".methods # => false
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
The joy of “self” discovery
p self
class MyClass
p self
def self.my_class_method P selfend
def my_instance_methodP 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)
what is “main” in ruby?
puts self
class Foo puts selfend
OUTPUT:mainFoo - 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.
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
protecteddef my_protected_methodend
publicdef my_public_method(other) self.my_protected_method other.my_protected_methodend
end
#=> Works#=> NoMethodError
mc1 = MyClass.newmc2 = MyClass.newmc1.my_public_method(mc2)mc1.my_protected_method
Quiz Time
class MyClass
protecteddef my_protected_methodend
def self.my_protected_method puts "Welcome" end
publicdef
my_public_method(other)
self.my_protected_method
other.my_protected_methodend
end
mc1 = MyClass.newmc2 = MyClass.new
mc1.my_public_method(mc2)MyClass.my_protected_method
=> Works=> Works
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.
Quiz Time
class Ay=1@p = 2@q@@r = 2
def initialize@@t=3@s = 2end
def welcome @indore = 1 @bhopal = 2 @jabalpur = 3 @@dhar = 3end
end
p A.instance_variables
p A.class_variables
a = A.newb = 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]
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 MyModuledef my_meth
puts “my_meth() of MyModule”end
end
Case 1: Include MyModule instance methods as instance methods of myClass
class MyClassinclude MyModule # Just include it…
end
Case 2: Include instance methods of MyModule as class methods of MyClass
Quiz Time
module MyModuledef self.my_freakin_meth
puts “my_freakin_meth() of MyModule”end
end
class MyClassinclude MyModule
end
MyClass.my_freakin_meth
What is the output?#NoMethodErrorLesson:When an object includes a moduleModule’s instance methods are includedModule’s class methods are excluded
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 ClassEg. String.instance_methods == “abc”.methods = > True String.methods == “abc”.methods => False due to inclusion of private and protected method.
Any Questions??????
Next Topics to cover:● Design Pattern● Practical Theory of Ruby
Object Model● Best Practices of Rails