[ next ] [ prev ] [ contents ] Invitation To Ruby

Inside/Out Refactoring

Suppose you have this ...

  1: def first_do_something
  2:   boring_boilerplate_code
  3:   more_boring_boiler_plate_code
  4:   something_interesting
  5:   more_code
  6:   finish_up_boring_code
  7: end
  8: def second_do_something
  9:   boring_boilerplate_code
 10:   more_boring_boiler_plate_code
 11:   another_interesting_thing
 12:   more_code
 13:   finish_up_boring_code
 14: end

This pattern repeats a lot, with the only line changing is the something_interesting function.

With Ruby, you can do this ...

  1: def do_something
  2:   boring_boilerplate_code
  3:   more_boring_boiler_plate_code
  4:   yield
  5:   more_code
  6:   finish_up_boring_code
  7: end
  8: 
  9: do_something { something_interesting }
 10: do_something { another_interesting_thing }


[ next ] [ prev ] [ contents ] Copyright 2002 by Jim Weirich.
All rights reserved.