Self, Class & Module
- Gourav Tiwari
# Object self example:
class User
def initialize(name)
@name = name
end
def user_name
@name
end
end
john = User.new('John'...
method_missing, superclass...
ram
Anonymous Class
superclass
Class def…
User Class
superclass
Class def...
Object Class
su...
Unusual way of defining a class
# class example:
User = Class.new do
def initialize(name)
@name = name
end
def user_name
@...
Self
# Class self example:
class User
# self is set here to User class
def initialize(name)
@name = name
end
def self.all
...
method_missing, superclass...
User
Anonymous Class
superclass
Class def…
class Class
superclass
Class def...
Module Class
...
Modules
● are not classes
● do not have instances
● are objects of Module class
● have module methods and module constants...
Modules: include
module MyModule
def obj_id
"The object id is #{object_id}"
end
end
class User
include MyModule
end
ram = ...
include makes a reference to module
ram
Anonymous Class
superclass
Class def…
User Class
superclass
Class def...
Object Cl...
Modules: extend
module MyModule
def class_name
"The class name is #{name}"
end
end
class User
extend MyModule
end
User.cla...
extend: includes module in Ano class
User
Anonymous Class
include MyModule
superclass
Class def…
class Class
superclass
Cl...
extend: includes module in Ano class
ram
Anonymous Class
include MyModule
superclass
Class def…
User Class
superclass
Clas...
Thank You!
Upcoming SlideShare
Loading in …5
×

Self, Class and Module

425 views
398 views

Published on

Published in: Education, Technology, Sports
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
425
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Self, Class and Module

  1. 1. Self, Class & Module - Gourav Tiwari
  2. 2. # Object self example: class User def initialize(name) @name = name end def user_name @name end end john = User.new('John') ram = User.new('Ram') def ram.soul 'divine' end self -> Current Object
  3. 3. method_missing, superclass... ram Anonymous Class superclass Class def… User Class superclass Class def... Object Class superclass Class def... BasicObject Class superclass Class def... Kernel Module Module def...
  4. 4. Unusual way of defining a class # class example: User = Class.new do def initialize(name) @name = name end def user_name @name end end john = User.name('John') ram = User.new('Ram') def ram.soul 'divine' end
  5. 5. Self # Class self example: class User # self is set here to User class def initialize(name) @name = name end def self.all 'All Users!' end end
  6. 6. method_missing, superclass... User Anonymous Class superclass Class def… class Class superclass Class def... Module Class superclass Class def... BasicObject Class superclass Class def... User Class superclass Class def... Object Class superclass Class def...
  7. 7. Modules ● are not classes ● do not have instances ● are objects of Module class ● have module methods and module constants ● mixes-in with classes
  8. 8. Modules: include module MyModule def obj_id "The object id is #{object_id}" end end class User include MyModule end ram = User.new ram.obj_id
  9. 9. include makes a reference to module ram Anonymous Class superclass Class def… User Class superclass Class def... Object Class superclass Class def... BasicObject Class superclass Class def... Kernel Module Module def... Anonymous Class superclass Class def… module MyModule Class def…
  10. 10. Modules: extend module MyModule def class_name "The class name is #{name}" end end class User extend MyModule end User.class_name user = User.new user.extend MyModule
  11. 11. extend: includes module in Ano class User Anonymous Class include MyModule superclass Class def… class Class superclass Class def... Module Class superclass Class def... BasicObject Class superclass Class def... User Class superclass Class def... Object Class superclass Class def...
  12. 12. extend: includes module in Ano class ram Anonymous Class include MyModule superclass Class def… User Class superclass Class def... Object Class superclass Class def... BasicObject Class superclass Class def... Kernel Module Module def... Anonymous Class superclass Class def… module MyModule Class def…
  13. 13. Thank You!

×