Dynamic programming asks for a dynamic mindset

(written by lawrence krubner, however indented passages are often quotes). You can contact lawrence at: lawrence@krubner.com, or follow me on Twitter.

Taryn East writes:

ruby encapsulation is for $%*t
Wow, I just learned that ruby encapsulation can be broken by simply using:

my_object.send(:my_secret_private_method)
That’s kinda useful to know if you’re trying to unit test a private method, but makes the concept of encapsulation totally meaningless in ruby.

Apparently, Ruby 1.9 revokes this “special” privilege, but then there are still many ways to use the power of ruby meta-programming to get to them anyway (see above link for some examples).

Paolo Perrotta writes about this in the book Metaprogramming Ruby. Page 84:

Breaking Encapsulation

At this point, you’re wearing a look of horror on your face. With a Context Probe you can wreak havoc on encapsulation! No data is private data anymore. “Isn’t that a Very Bad Thing?” you ask.

Pragmatically, there are some situations where encapsulation just gets in your way. For one, you might want to take a quick peek inside an object from an irb command line. In a case like this, breaking into the object with instance_eval() is often the shortest route.

If Ruby programmers find this alarming, then Java programmers might smugly proclaim us justly punished for our crimes. Those who enjoy the benefits of dynamic programming need to re-program their brains to accept all of what that means.

Post external references

  1. 1
    http://rubyglasses.blogspot.com/2011/06/ruby-encapsulation-is-for-t.html
  2. 2
    http://www.amazon.com/Metaprogramming-Ruby-Program-Like-Pros/dp/1934356476
Source