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

About this user

James Robertson http://www.r0bertson.co.uk

« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS 

Declaring a private method in Ruby

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.
« Newer Snippets
Older Snippets »
Showing 1-1 of 1 total  RSS