1. 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
          @protected_variable
        end
      end
      

      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.

    1. 2

      As a ruby-ist who has tried to teach myself haskell before, this is really helpful. I hope there’s a part 2 and beyond coming soon.

      Generally what happens with me is that I pick it up, and at some point lose steam. In the mean time, I’ve gotten so much better with recursion and function composition that coding in ruby is really exciting again. It’s not that I hit a brick wall learning haskell, more that I run into some mud. And all that mud training makes running in the places I’m used to easier and more appealing. So I’ve always felt that my time spent learning haskell was very useful, but at some point I’d like to feel like I actually know the language.

      In my previous attempts, I’ve missed rspec. A lot. I’ve hated having to load up ghci to see which functions aren’t working the ways I think they should. I knew there were testing frameworks, but I had no idea you could get one so close to rspec’s fantastic syntax. I suspect hspec will be a big help toward keeping me focused next time I try to pick up haskell.