ruby object design
Post on 12-Jul-2015
106 Views
Preview:
TRANSCRIPT
Ruby Object Design
Eddie Li (ADZ)
eddie@visionbundles.com
http://adz.visionbundles.com
Github: afunction
Facebook: /adz.624
A difference between Ruby object and other languages
1. syntax omission
Cons
1. Make your codes more readable to a human. (model validation, routes, rspec, view helper)
Pros
1. High learning curve for beginners. (Easy to misunderstanding)
2. No REAL propertyclass/instance variables
and method instead.
attr_accessor / attr_reader / attr_writer
Access via class/instance variable
3. Operator is also a methodmagic!
def +def -def *def /
def << def ==
def ===
4. Open Class
5. Meta-programming
Generate code … by code
wrapper abstraction behavior
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.
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.
class_evaldefine_method
define_singleton_method
6. module & classlike PHP Traits
multi inheritancebreak top down inheritance
a. DRY duplicate method
b. callback when some class included
“base” means the class that include this module.
It can help you…
1. Modular behavior2. Modular abstraction behavior with meta-programming3. Reduce code duplication
Pros
1. Easy to organize a huge/fat class2. Design/extend object flexible
Cons
1. Hard to read and write unit-test (when modules with high dependent context)
2. Hight learning curve3. Easy to misleading
7. skip “private, protected” method protection
“send” method will skip “private, protected
combine send and include method with module
extend class outside without changing exists code.
Live Coding.aka Demo!
Q&A Thank you.
top related