Singleton method/class in Ruby

dieutb
May 29, 2021

--

For example, we have many instances of class String and we want to define some special methods for only a specific instance, but don’t want the others (same class) have these methods. Ruby supports us to do this by the singleton class.

  • Is used for extending methods for an specific instance (note thatmodule, class also are instances of Module, Class)
  • To get the instance’s singleton class, call [instance].singleton_class
  • To see which function is belonged to the singleton class called [instance].singleton_methods
  • The singleton class could also have its singleton class

References

--

--

dieutb
dieutb

Written by dieutb

passion in finding solutions and research some things relate to technical and helpful in life. Also strongly interest in foods, travel, and gentle music :)

No responses yet