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
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)
2. No REAL property 
class/instance variables 
and 
method instead.
attr_accessor / attr_reader / attr_writer
Access via class/instance variable
3. Operator is also a method 
magic!
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_eval 
define_method 
define_singleton_method
6. module & class 
like PHP Traits 
multi inheritance 
break 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 behavior 
2. Modular abstraction behavior with meta-programming 
3. Reduce code duplication
Pros 
1. Easy to organize a huge/fat class 
2. Design/extend object flexible
Cons 
1. Hard to read and write unit-test 
(when modules with high dependent context) 
2. Hight learning curve 
3. 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.

Ruby Object Design