instance variable vs class variable


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



Copy this code and paste it in your HTML
  1. class App
  2. $super_count = 111
  3. @@count = 0
  4.  
  5. def initialize (caller)
  6. @call = caller
  7. end
  8.  
  9. # instance method
  10. def callme
  11. p “called using #{@call}”
  12. @@count+=1
  13. end
  14.  
  15. # class method
  16. def App.callmetoo
  17. p “called using callmetoo”
  18. @@count+=1
  19. end
  20.  
  21. end
  22.  
  23. obj1 = App.new(”OBJECT1″)
  24. obj2 = App.new(”OBJECT2″)
  25. p obj1.callme
  26. p obj2.callme
  27. p App.callmetoo
  28. p $super_count #can be access outside the class
  29. #p App.@@count #cant access outside the class

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.