Ruby/Methods Classes Modules Namespaces4

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
** Pass Arguments in Ruby
** More Ruby Classes
** Ruby Advanced Classes
** Ruby Inheritance
** Ruby Class Explaination
** Ruby Methods
** Ruby Modules and 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 4

Allow me to explain now what the code does:


The class is created with the name "Displaying".

The initialize method is the constructor, you use it to create new objects and configure them.You must have a constructor for a class to function properly.The constructor is given a parameter or an argument, "msg" in our case.The "msg" will be in fact stored in a variable called "message".

For the purpose of our new class that is supposed to return "something" we created a method that will return us the message, method named "showmessage".

We've then created a new class object using the "new" method on the class Displaying and assigned this to the variable called "display", not before passing the message itself ("this is a new message") to the class constructor.

To print the message on screen, we've used a "puts" instruction along with using the "showmessage" method on the "display" variable we've recently created.

The variable prefixed with a "@" sign is an instance variable.A class variable should begin with double "@@" signs.The benefit in using class variables is that of them being shared by all instances of the class.If modified inside an object, the new value would be the same for the rest of the objects.When you modify an instance variable from an object, it's new value is only available to the same object.

[edit] More advanced:


The "msg" variable is called an instance variable.The "showmessage" is an "accessor", this is how methods that are used for accesing values inside an object.

Classes have basic functioning principles in all object-oriented programming languages.Please reffer to this Wikipedia article for more general info.

In consequence, as in other programming languages, "objects" inside a class can have 3 properties : they can either be readable, writable, or readable/writable.

Readable only means that the certain object can only be read.

Writable means that the objects can be modified.

Readable/writeable is similar to writeable.

Lets learn more about these attributes in Ruby:

In our example, "showmessage" from display.message is an attribute, a readable attribute since the value of the @message variable if you use it.

Another way of writing the code on the previous page(this time explicitly setting the message variable as readable):

class Displaying
 attr_reader :message
 def initialize(msg)
 @message = msg
 end
 
 def showmessage
     return @message
 end 
end
 
display = Displaying.new("this is a test message")
puts display.showmessage

[edit] How to create Ruby writable attributes

Let's imagine that you're creating a certain application and you want to give the user the ability to change font sizes.

class Font
attr_reader :size
def size=(size)
@size = size
end
def initialize(size)
@size = size
end
end
 
font = Font.new(“small”)
puts “The new font size is “ + font.size
font.size = “medium”
puts “The updated font size is “ + font.size

What you can notice from the code above is that the way to make writable attributes is by placing a "=" sign after the accessor method (discussed above), in our case, the "size" accessor method.

[edit] Another way of creating Ruby writable attribute


class Font
attr_reader :size
attr_writer :size
def initialize(size)
@size = size
end
end
font = Font.new(“small”)
puts “The new font size is “ + font.size
font.size = “medium”
puts “The updated font size is “ + font.size


Previous Next