Created
November 27, 2019 15:00
-
-
Save cyrilchampier/05d915c6572683e97cba5bd1a5c19085 to your computer and use it in GitHub Desktop.
singleton class
This file contains 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 | |
class Vehicule | |
def move | |
'I move' | |
end | |
end | |
# Singleton Class | |
class << Vehicule | |
def available_colors | |
%w(red green blue) | |
end | |
end | |
class Car < Vehicule | |
def move | |
'I drive' | |
end | |
end | |
class << Car | |
def available_colors | |
super + %w(black) | |
end | |
end | |
something_that_moves = Car.new | |
puts something_that_moves.move | |
def something_that_moves.clignote | |
'blibngbling' | |
end | |
class << something_that_moves | |
def super_clignote | |
'blibngbling' | |
end | |
end | |
class << something_that_moves.singleton_class | |
def super_super_clignote | |
'blibngbling' | |
end | |
end | |
puts something_that_moves.clignote | |
puts something_that_moves.super_clignote | |
puts Car.available_colors.to_s | |
puts "something_that_moves.class.ancestors: #{something_that_moves.class.ancestors}" | |
puts "Car.ancestors: #{Car.ancestors}" | |
puts "Car.singleton_class.ancestors: #{Car.singleton_class.ancestors}" | |
puts "Car.singleton_class.object_id: #{Car.singleton_class.object_id}" | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment