Don't the Ruby methods instance_eval() and send() negate the benefits of private visibility?

后端 未结 5 921
误落风尘
误落风尘 2021-01-21 12:45
w = Widget.new # Create a Widget
w.send :utility_method # Invoke private method!
w.instance_eval { utility_method } # Another way to invoke it
w.instance_eval { @x } # R         


        
5条回答
  •  北荒
    北荒 (楼主)
    2021-01-21 13:30

    ruby believes in giving you the power to do what you want. it just doesn't make it easy to inadvertently shoot your foot off - if you want to subvert the private declarations, you have to use syntax that makes it clear you are doing so. note that the person finally deciding what the code should or shouldn't do is the person using a library, not the person writing it.

提交回复
热议问题