Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Declaring a private method in Ruby (See related posts)

The following example code shows how to make a method private in Ruby. source: Ruby Programming/Syntax/Classes [wikibooks.org]


Simple example:
 class Example
   def methodA
   end
   
   private # all methods that follow will be made private: not accessible for outside objects
   
   def methodP
   end
 end

If private is invoked without arguments, it sets access to private for all subseqent methods. It can also be invoked with named arguments.

Named private method example:
 class Example
   def methodA
   end
   
   def methodP
   end
   
   private :methodP
 end

Here private was invoked with an argument, altering the visibility of methodP to private.

You need to create an account or log in to post comments to this site.


Click here to browse all 5137 code snippets

Related Posts