Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby?
There's no requirement
for you to use the words "public", "private" and "protected" in Ruby - methods within classes that you write default to being public anyway. But you may
wish to protect certain of your methods from being called directly by the users of your classes.
So - what protection do they give?
If you give a private
command, then it means that the following method(s) up to the next protected
command are only callable without an explicit object reference.
. So this means that you must be within the class when you call them, and furthermore you must call them on the current object. adding self. in front of a call wouldn't change the meaning
, but it would lead to NoMethodError exception.
If you give a protected
command, then it means that the following method(s) up to the next private
command are only accessible when called from within methods in the current class, or from methods within classes which are derived (extended) from that class.
Example - see [here]
from last week's Ruby course.
As well as the use of "private" (especially) being rather different in Ruby to other languages, you'll note that Ruby has no concept of an abstract class - it doesn't need one, though there are some online examples if you want to write a class that forces actions on anyone extending it.
You'll also note that Ruby does not offer multiple inheritance, nor interfaces. But it does offer mixins which allow you to pull in additional common code into multiple classes in addition to inheritance. See [here]
for an article on that topic, and examples. (written 2012-06-23, updated 2012-06-30)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articlesR108 - Ruby - More Classes and Objects 
Testing your new class - first steps with cucumber - (2015-10-23) 
Build up classes into applications sharing data types in Ruby - (2015-10-23) 
Where does Ruby load modules from, and how to load from current directory - (2015-06-03) 
Changing what operators do on objects - a comparison across different programming languages - (2014-12-26) 
Standard methods available on all objects in Ruby - (2012-06-23) 
Why you should use objects even for short data manipulation programs in Ruby - (2012-06-10) 
Ruby - a training example that puts many language elements together to demonstrate the whole - (2011-04-23) 
Ruby training - some fresh examples for string handling applications - (2011-02-05) 
Changing a class later on - Ruby - (2011-02-02) 
Private and Public - and things between - (2011-01-22) 
Ruby - examples of regular expressions, inheritance and polymorphism - (2010-10-02) 
What is a factory method and why use one? - Example in Ruby - (2010-09-30) 
The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11) 
Object Oriented Ruby - new examples - (2010-02-03) 
Direct access to object variable (attributes) in Ruby - (2010-02-02) 
Defining a static method - Java, Python and Ruby - (2010-02-01) 
Tips for writing a test program (Ruby / Python / Java) - (2010-01-29) 
Ruby objects - a primer - (2010-01-29) 
Ruby - is_a? v instance_of? - what is the difference? - (2010-01-27) 
Object Orientation in Ruby - intermediate examples - (2009-07-16) 
Some Ruby programming examples from our course - (2008-03-21) 
What are factory and singleton classes? - (2007-06-04) 
Think about your design even if you don't use full UML - (2006-03-24) 
MTBF of coffee machines - (2005-01-20)R116 - Ruby - Security Issues. 
Code quality counts - (2006-11-26)
Some other Articles
Programming languages - what are the differences between them?Steam train calls at Melksham - PicturesLoad path, load and require in Ruby, and a change from 1.8 to 1.9Private, Protected, Public in Ruby. What about interfaces and abstract classes in Ruby?Ruby of Rails - cleanly displaying model data in the viewAdding validation to form entries and sticky fields - Ruby on RailsProviding a form to allow the user to add data to the model - Ruby on RailsMultiple views in a single appication - sharing common parts of the template - Ruby on RailsSome traps it's so easy to fall into in designing your web site