use define_method to remove duplicate codes
move method definition closer to example
Merge pull request #1 from 404pnf/simpler

use define_method to remove duplicate codes