Skip to content

Ruby Access Control - Are Private And Protected Methods Only A Guideline?

skorks.com
10 pointsskorks2 comments
On HN

Comments

I was just writing a bit of code in javascript (http://github.com/dsimard/jskataUndo/blob/master/jskataUndo....) when I saw this post on HN. I was wondering "should I make some functions private?" and the answer is "NO". Why would I do this? Is this because I don't trust programmers? Why should I care if someone messes up with my "private" things? It's his problem after all. If you look at the source, you'll see that I have a private section at the end but it means "you don't need to use this to make it work".

(note : there are some ways to make things private in javascript as I wrote there : http://www.javascriptkata.com/2009/09/23/how-to-create-an-ob...)

tl;dr -> Ruby allows you to bypass private/protected by using Object#send

    class Object
      private 
      def name
        puts "Name called"
      end
    end

    o = Object.new
    o = o.send :name # outputs "Name called"
And therefore you can easily test private methods.
AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.