Change how define_method is used to support ruby 2.1.1

Closes #2177
Merge branch 'v1.12.x' into next