Ruby/Ruby Syntax and Semantics

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
Ruby syntax and semantics

Ruby similar to other programming languages follows a set of semantics and syntax practises to make programming more professional and readable.

We will have a look on few of them one by one in this section below:

1. Naming

In ruby we always mention the class names and module names with an upper case letter .It means that the classes and modules should always start with the capital letter.

For example:

module Printable module DeliveryDate class StringInputMethod class StringReplace class PMB

Method names start with a lower case letter def cbrt def abp def puts


2. Iterators

We should always use, braces ({…}) for a single line of code blocks

8.times {j| puts j}

We should use, do…end for situations where we need to put multiple lines in iteration

8.times do |j| puts j puts Math.sqr(j) end


3. Parentheses And Indentation

Parentheses: It is strictly recommended to put the regular expressions and arithmetic expressions always in the parentheses as it avoids lot of confusion and errors while writing the code and thus helps to avoid the syntax errors.

puts ((7-5) * 2))

This will give the output as 4 which is the actual expected answer to this expression

puts (7+5) * 4

This will not give the expected result as the output but will display an undefined method for nil (NameError) as it doesn’t accept * 4 as part of the parameter list since the parentheses were missing in this statement.

Indentation: In ruby it is advised to start writing the code with extreme left with no indents. Two spaces are left where we have to put a Sub-indent.

4. Identifiers

Identifiers:Identifier is the term given to all the various alphabets, decimal digits along with the underscore characters that are used to represent the identity of the variables, constants etc. Each identifier starts either with the alphabetic character or the underscore. The length of the identifiers in Rubya re not restricted to any specific length.

Examples:

World ruby_is_a_fun_to_work


5. Variables Variables:These are again the identifiers which are used in Ruby programming to give names or identification to the values we access while programming. These Variables help us to differentiate one unique value from the other.The Vraiables can be either the local variables, Global Variables or Instance Variables. The Vraiable name in Ruby can go to any length but it is recommended to always keep the variable names short and meaningful as it makes easy to remember them while programming.

Global Variables

The variable name which starts with the $ sign is a global variable which means that it can be accessed from any anywhere in the program.Their scope remains alive till the program is alive and is in execution.

Examples:

$school
$/

Instance variables

Instance Variables are again the identifiers which always begin with the "@" character. This symbol denotes that this variable is an instance of itself.The variables which are not initialized have NIL value.

Examples:

@school
@city


6. Constants Constants: The Identifiers in Ruby which are written in upper case letters or capital letters represent the constants in Ruby. These are defined in the class defining section of the program i.e we declare the constants at the time of defining the class. The constants should always have a assigning value and they can never be left Nil unlike Variables.

Examples:

CITY
SCHOOL


Next
Personal tools