Created
June 10, 2019 20:19
-
-
Save srinidhi-lwt/20c6d15840cf2829410d706bf576c1c6 to your computer and use it in GitHub Desktop.
Ruby Method Hierarchy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Reviewer | |
def name | |
puts "Hi. This is Reviewer" | |
super rescue nil | |
end | |
end | |
module Manager | |
def name | |
puts "Hi. This is Manager" | |
super rescue nil | |
end | |
end | |
module Analyst | |
def name | |
puts "Hi. This is Analyst" | |
super rescue nil | |
end | |
end | |
class Employee | |
# inherited method to class Employee | |
def name | |
puts "Hi. This is Employee " | |
super rescue nil | |
end | |
end | |
class Developer < Employee | |
include Reviewer # including method into class Employee | |
prepend Analyst # prepending method to class Employee | |
# instance method to class Employee | |
def name | |
puts "Hi. This is Developer" | |
super rescue nil | |
end | |
end | |
developer_1 = Developer.new | |
def developer_1.name | |
puts "Hi. This is developer_1" | |
super | |
end | |
developer_1.extend(Manager) | |
developer_1.name | |
=> 'Hi. This is developer_1' | |
'Hi. This is Manager' | |
'Hi. This is Analyst' | |
'Hi. This is Developer' | |
'Hi. This is Reviewer' | |
'Hi. This is Employee' | |
developer_1.class.ancestors | |
=> [Analyst, Developer, Reviewer, Employee, Object, Kernel, BasicObject] | |
# [singleton, extend, prepend, class instance, include, super] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment