Skip to content

Instantly share code, notes, and snippets.

@cyrilchampier
Created November 27, 2019 15:00
Show Gist options
  • Save cyrilchampier/05d915c6572683e97cba5bd1a5c19085 to your computer and use it in GitHub Desktop.
Save cyrilchampier/05d915c6572683e97cba5bd1a5c19085 to your computer and use it in GitHub Desktop.
singleton class
# 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