Last active
May 15, 2019 10:36
-
-
Save omedale/1dc8e385b90406bd4511aafed782d57c to your computer and use it in GitHub Desktop.
OOP - (Class, Inheritance, Encapsulation, Extend, Include, Module)
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 FormatString | |
| def to_slug(str) | |
| str.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '') | |
| end | |
| end | |
| module DateTime | |
| def now | |
| Time.now | |
| end | |
| end | |
| class Base | |
| include FormatString | |
| extend DateTime | |
| def initialize | |
| @name = "Base" | |
| end | |
| def self.is_class_method | |
| puts "I am a class method" | |
| end | |
| def is_instance_method | |
| puts "I am an instance method" | |
| end | |
| def implicit_private | |
| private_method | |
| end | |
| def explicit_private | |
| self.private_method | |
| end | |
| def implicit_protected | |
| protected_method | |
| end | |
| def explicit_protected | |
| self.protected_method | |
| end | |
| private | |
| def private_method | |
| puts "private method called" | |
| end | |
| protected | |
| def protected_method | |
| puts "protected_method called" | |
| end | |
| public | |
| def get_name | |
| @name | |
| end | |
| end | |
| #In Ruby private data members and methods are inherited. | |
| class Derived < Base | |
| def public_method | |
| private_method | |
| protected_method | |
| end | |
| end | |
| obj = Derived.new | |
| obj.public_method | |
| b = Base.new | |
| puts Base.now | |
| puts b.implicit_private | |
| # puts b.explicit_private | |
| puts b.implicit_protected | |
| puts b.explicit_protected | |
| puts b.is_instance_method | |
| puts Base.is_class_method | |
| puts obj.get_name | |
| puts obj.to_slug("femda femi do") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment