Ruby/Methods Classes Modules Namespaces7

From Meshplex

Jump to: navigation, search
Image:Ruby_on_rails_tutorials.jpg
Ruby for complete beginners
Ruby Introduction
What can I use RoR for?
Reasons for choosing RoR over other popular programming languages such as php or asp.net .What makes Ruby so much more special
Where can I find RoR? In what “forms” does it come?
How to install RoR.Solutions for both the novice and professional programmers on Windows,Mac OS X and Linux.Prerequisites.
Ruby programming tutorials for beginners:
Ruby Basics
Ruby Variables, Datatypes, Operators
Ruby Symbols
Ruby Statements
Ruby Converting data to another type: type conversion or typecasting
Ruby Arrays, Hashes, Ranges
Ruby Functions and built in functions
Ruby Control structures
Ruby Regular expressions and blocks
Ruby Loops
Ruby Recursion
Ruby Data Structures
Ruby Methods, Classes, Modules, Namespaces
Ruby Exceptions
Ruby Object Oriented Programming
Ruby Multithreading
Ruby File Handling.Input and Output
Ruby Basic GUI
Ruby and databases.Ruby on Rails and MySQL
Ruby Basic CGI.Using fastCGI
Ruby Basic Networking and web programming
Ruby Basic Graphics
Ajax and Rails.Web 2.0 and what it means
Ruby Testing, Debugging, Automation of tasks
Ruby Apache,Capistrano, Mongrel,lighttpd – reviews and tips
Finding a Ruby on Rails ready web hosting company
BONUS: mini tutorial for a simple RoR application

Contents

[edit] Ruby Methods, Classes, Modules, Namespaces page 7

[edit] Protected access


class Font
def initialize(size)
@size = size
end
 
protected
def showsize
return @size
end
end
 
class ChangeSize < Font
def initialize(size)
@font = Font.new(size)
end
 
def shownewsize
return @font.showsize
end
end


Watch what happens when you try to access a protected method from outside the class:


Image:Class5.gif

[edit] How to override Ruby restriction levels


Of course, you can bypass restriction levels by redefining the method in a related class. If you're familiar with how CSS definition rules work for example, you will easily understand how to apply in Ruby too. Basically, the last definition of a method is taken into consideration, so if you're defined a class as protected in the main class but then as private in a derived class, the method will be treated as private.

The same principle applies for other redefined characteristics inside a method.


[edit] Ruby Class Methods

Let's say you're in a hurry and just want to get your work done and seeing results. You're not particularly interested in saving the data your program is supposed to work on, so you pick a shortcut: class methods. Class methods have the advantage that they can be used without defining any objects first. The disadvantage is that you cannot store the data the class is supposed to manipulate because there are no objects to store it in.

Let's take this sample:

You're trying to teach your kid how to do multiplications. Here's a simple class method to do it.

class Multiply
def Multiply.mul(first, second)
return first * second
end
end
 
puts Multiply.mul

So you can only use class methods to compute something.

Image:Class6.gif


Previous Next