Created
February 21, 2013 15:19
-
-
Save yvesvanbroekhoven/5005401 to your computer and use it in GitHub Desktop.
Ruby singleton & inheritance
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
| class Parent | |
| def self.name(name) | |
| @name = name | |
| puts @name | |
| end | |
| def whoami | |
| puts "I'm the parent #{@name}" | |
| end | |
| name('dad') | |
| end | |
| class Child < Parent | |
| def self.name | |
| super | |
| end | |
| end |
Author
class Parent
def self.name(name = nil)
if name
@name = name
else
@name
end
end
def whoami
puts "I'm #{self.class.name}"
end
name('dad')
end
class Child < Parent
def self.name(name = nil)
super
end
name('babydoll')
end
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Parent(self) dat nu toevallig ook eenClassis.'dad'printen omdat dat word uitgevoerd op een object dat eenParentis.Maw:
Parent.new.class => ParentParent.class => Class