rubyaccess-specifier

Private/Protected block in Ruby?


Ruby doesn't seem to have a facility for defining a protected/private block like so:

protected do
  def method
  end
end

This would be nice compared to

protected 

def method 
end 

public

where you might forget to "public" after the protected methods.

It seems possible to implement this using metaprogramming. Any ideas how?


Solution

  • Since you want to group by functionality you can declare all your methods, and then declare which ones are protected and private by using protected followed by the symbols of the methods you want to be protected, and the same for private.

    The following class shows what I mean. In this class all methods are public except bar_protected and bar_private which are declared protected and private at the end.

    class Foo
    
      def bar_public
        print "This is public"
      end
    
      def bar_protected
        print "This is protected"
      end
    
      def bar_private
        print "This is private"
      end
    
      def call_protected
        bar_protected
      end
    
      def call_private
        bar_private
      end
    
      protected :bar_protected
    
      private :bar_private
    
    end