1. 4

  2. 2

    “The instance_variable_set method is the bane of anyone who wants to truly protect their object’s state. Because of it’s existence, it is basically impossible to truly hide your objects’ internal state in Ruby.”

    Why not override that method if you don’t like how it behaves?

    1. 1

      Even if you override that method, someone trying to break into your object can still grab it from elsewhere using the “method” method, and then reattach it to the object. But regardless of instance_variable_set, you can always reopen the class and add a method that accesses the instance variable. E.g.

      class PreviouslyDefinedClass
        def get_protected_variable

      Basically, if you use a variable with the @ symbol, it’s just about impossible to hide.

      1. 1

        Good point.

        Still, if someone is so willing to jump through hoops to get around access control they can do the essentially the same in JavaScript too.

        It’s not “instance_variable_set” that’s the problem, it’s just the nature of dynamic languages and reasonable expectations.