Singleton classes


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. # How to define and use a singleton class
  2. # - modifies the class's class
  3. # - instance methods in the singleton class are class methods externally
  4.  
  5. class TheClass
  6. class << self
  7. def hello
  8. puts "hi"
  9. end
  10. end
  11. end
  12.  
  13. # invoke a class method
  14. TheClass.hello # hi

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.