Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Ruby Object Design

357 views

Published on

Ruby Object Design

Published in: Technology
  • Be the first to comment

Ruby Object Design

  1. 1. Ruby Object Design
  2. 2. Eddie Li (ADZ) eddie@visionbundles.com http://adz.visionbundles.com Github: afunction Facebook: /adz.624
  3. 3. A difference between Ruby object and other languages
  4. 4. 1. syntax omission
  5. 5. Pros 1. Make your codes more readable to a human. (model validation, routes, rspec, view helper) Cons 1. High learning curve for beginners. (Easy to misunderstanding)
  6. 6. 2. No REAL property class/instance variables and method instead.
  7. 7. attr_accessor / attr_reader / attr_writer
  8. 8. Access via class/instance variable
  9. 9. 3. Operator is also a method magic!
  10. 10. def + def - def * def / def << def == def ===
  11. 11. 4. Open Class
  12. 12. 5. Meta-programming Generate code … by code
  13. 13. wrapper abstraction behavior
  14. 14. Pros 1. Split logics to different level and make your business logic clean. 2. Increase productive. 3. Follow DRY code principle like there is no limitation.
  15. 15. Cons 1. Hard to maintain (but it can be solve) 2. Hard to name variables and methods for abstraction behavior. 3. Hard to understand abstraction behavior codes from time to time.
  16. 16. class_eval define_method define_singleton_method
  17. 17. 6. module & class like PHP Traits multi inheritance break top down inheritance
  18. 18. a. DRY duplicate method
  19. 19. b. callback when some class included “base” means the class that include this module.
  20. 20. It can help you… 1. Modular behavior 2. Modular abstraction behavior with meta-programming 3. Reduce code duplication
  21. 21. Pros 1. Easy to organize a huge/fat class 2. Design/extend object flexible
  22. 22. Cons 1. Hard to read and write unit-test (when modules with high dependent context) 2. Hight learning curve 3. Easy to misleading
  23. 23. 7. skip “private, protected” method protection
  24. 24. “send” method will skip “private, protected
  25. 25. combine send and include method with module extend class outside without changing exists code.
  26. 26. Live Coding. aka Demo!
  27. 27. Q&A Thank you.

×